Professional Web Applications Themes

array and for clarification. - PHP Development

I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m[$i]); reset($m); } This doesn't work, it says: Warning: array_shift(): The argument should be an array in ./tool.php on line 31 However, this does work: $c = count($m); for ($i = 0; $i <= $c; $i++) { array_shift($m[$i]); } Why can't I reference the count of $m in the for without it crapping out, but if I make a variable it is fine? $m doesn't change, so even if it recounted $m each time, it would ...

  1. #1

    Default array and for clarification.

    I'm diddlying with a script, and found some behavior I don't understand.

    Take this snippet:

    for ($i = 0; $i <= count($m); $i++) {
    array_shift($m[$i]);
    reset($m);
    }


    This doesn't work, it says:
    Warning: array_shift(): The argument should be an array in ./tool.php on
    line 31


    However, this does work:
    $c = count($m);
    for ($i = 0; $i <= $c; $i++) {
    array_shift($m[$i]);
    }

    Why can't I reference the count of $m in the for without it crapping out,
    but if I make a variable it is fine? $m doesn't change, so even if it
    recounted $m each time, it would be the same.. I'd think.

    Many thanks.

    --Brian


    Brian Guest

  2. #2

    Default Re: array and for clarification.

    Brian wrote:
    > I'm diddlying with a script, and found some behavior I don't understand.
    >
    > Take this snippet:
    >
    > for ($i = 0; $i <= count($m); $i++) {
    > array_shift($m[$i]);
    > reset($m);
    > }
    >
    > This doesn't work, it says:
    > Warning: array_shift(): The argument should be an array in ./tool.php on
    > line 31
    >
    > However, this does work:
    > $c = count($m);
    > for ($i = 0; $i <= $c; $i++) {
    > array_shift($m[$i]);
    > }
    >
    > Why can't I reference the count of $m in the for without it crapping out,
    > but if I make a variable it is fine? $m doesn't change, so even if it
    > recounted $m each time, it would be the same.. I'd think.
    Quote from [url]http://www.php.net/array_shift[/url]
    "array_shift() shifts the first value of the array off and returns it,
    shortening the array by one element and moving everything down. All
    numerical array keys will be modified to start counting from zero while
    literal keys won't be touched."

    Therefore, the array's count *does* change. When you shift it the first
    time, the count is reduced by one. so check this:

    <?php
    $m=array(array(0),array(1),array(2));
    print_r($m);
    for ($i = 0; $i <= count($m); $i++) {
    array_shift($m[$i]);
    reset($m);
    echo $i,' ';
    print_r($m);
    if($i==3) break;
    }
    ?>

    Output:
    Array
    (
    [0] => Array
    (
    [0] => 0
    )

    [1] => Array
    (
    [0] => 1
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    0 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    [0] => 1
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    1 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    2 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    )

    )
    PHP Warning: array_shift(): The argument should be an array in file.php
    on line 5
    3 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    )

    [3] =>
    )

    So, when $i==0, shift the $m[0] array, you get $m[0]== array();

    count($m) still == 3...

    When $i==1, shift the $m[1] array, you get $m[1]== array();
    When $i==2, shift the $m[2] array, you get $m[2]== array();

    Because you have $i<=count($m), $i==3 is valid (there isn't a 3rd
    element, which causes your warning message)...

    When $i==3, shift the $m[3] array, you get $m[3]== '';

    Now count($m)==4 ... You have created an infinate loop...

    However, changing "$i<=count($m)" to "$i<count($m)" you get this:

    Array
    (
    [0] => Array
    (
    [0] => 0
    )

    [1] => Array
    (
    [0] => 1
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    0 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    [0] => 1
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    1 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    [0] => 2
    )

    )
    2 Array
    (
    [0] => Array
    (
    )

    [1] => Array
    (
    )

    [2] => Array
    (
    )

    )

    Hope this helps!

    --
    Justin Koivisto - [email]spamkoivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

  3. #3

    Default Re: array and for clarification.

    > Hope this helps!

    No, this only explains the whole thing to me. Thanks for taking the time to
    break that down, I understand how that works now.

    Much obliged!

    --Brian


    Brian Guest

Similar Threads

  1. CFAPPLICATION clarification
    By jdeline in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: May 20th, 05:22 PM
  2. CFHTTP help/clarification
    By davidmedifit in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: April 6th, 03:23 PM
  3. Clarification on SBSPACES
    By KalpanaPai in forum Informix
    Replies: 6
    Last Post: August 26th, 06:15 AM
  4. Clarification please
    By John Carlson in forum Informix
    Replies: 12
    Last Post: August 23rd, 03:43 AM
  5. Clarification
    By Sam Poikail in forum Macromedia Fireworks
    Replies: 0
    Last Post: August 15th, 04:40 AM

Bookmarks

Posting Permissions

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

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139