Welcome to Discuss Everything Forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.


 

Tags for this Thread

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    dhvrm
    dhvrm's Avatar
    Guest
    You are probably iterating the array with a for loop, rather than a foreach.

    <?php
    $taken = array(1, 2, 3, 8, 9, 10, 11);
    $avail = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32);
    $result = array_diff($avail, $taken);

    //notice that $result array is indexed
    //with larger array's indexes;
    print_r($result);
    echo "<br>";

    //$result array has 25 items;
    //thus, using for loop restricted by count,
    //only cells 1-25 are output;
    //cells with address > 25 aren't included
    for($i = 0; $i < count($result); $i++) {
    echo "$result[$i], ";
    }
    echo "<br>";

    //foreach returns all cells in array
    //without regard for their index
    foreach($result as $output) {
    echo "$output, ";
    }
    ?>

  2. #2
    Edwin M
    Edwin M's Avatar
    Guest

    missing array items with PHP function array_diff()?

    I have two arrays:

    Taken: 1, 2, 3, 8, 9, 10, 11,

    Total: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,

    The operation I do on them is this:
    $remaining = array_diff($total, $taken);

    the return of printing $remaining is this:
    Remaining: 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,

    Why are 26 through 32 missing?
    Another example:

    two arrays:

    Range: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
    Taken: 1, 2, 3, 8, 9, 10, 11,

    the operation I perform on them:
    $selected = array_diff($range, $taken);

    the output:
    Selected: 7, 12, 13, 14, 15,

    Why are 16 through 19 missing?

  3. #3
    dhvrm
    dhvrm's Avatar
    Guest
    You are probably iterating the array with a for loop, rather than a foreach.

    <?php
    $taken = array(1, 2, 3, 8, 9, 10, 11);
    $avail = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32);
    $result = array_diff($avail, $taken);

    //notice that $result array is indexed
    //with larger array's indexes;
    print_r($result);
    echo "<br>";

    //$result array has 25 items;
    //thus, using for loop restricted by count,
    //only cells 1-25 are output;
    //cells with address > 25 aren't included
    for($i = 0; $i < count($result); $i++) {
    echo "$result[$i], ";
    }
    echo "<br>";

    //foreach returns all cells in array
    //without regard for their index
    foreach($result as $output) {
    echo "$output, ";
    }
    ?>

 

 

Quick Reply Quick Reply

Click here to log in


What color is our footer?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 08-09-2011, 11:45 PM
  2. Replies: 1
    Last Post: 08-09-2011, 11:44 PM
  3. php mysql and array function?
    By Unknown in forum Discuss PHP
    Replies: 0
    Last Post: 10-31-2009, 01:10 AM
  4. Complex Array function for PHP?
    By Danzamagu in forum Discuss PHP
    Replies: 0
    Last Post: 10-12-2009, 08:47 PM
  5. Replies: 0
    Last Post: 08-23-2009, 01:44 PM

Bookmarks

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •