By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,737 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,737 IT Pros & Developers. It's quick & easy.

array and for clarification.

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 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 - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #2

P: n/a
> 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
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.