Connecting Tech Pros Worldwide Forums | Help | Site Map

array and for clarification.

Brian
Guest
 
Posts: n/a
#1: Jul 17 '05
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



Justin Koivisto
Guest
 
Posts: n/a
#2: Jul 17 '05

re: array and for clarification.


Brian wrote:[color=blue]
> 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.[/color]

Quote from http://www.php.net/array_shift
"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 - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Brian
Guest
 
Posts: n/a
#3: Jul 17 '05

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


Closed Thread