473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jul 17 '05 #1
2 2747
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
4
by: Alan Foxmore | last post by:
Hi everyone, I'm new to C# and I was hoping I could get some clarification on the syntax for jagged and multidimensional arrays. Here is my question: The following syntax is correct for...
9
by: AtariPete | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
2
by: Nathan Sokalski | last post by:
I have a multidimensional array declared as the following: Dim guesses(14, 5) As Integer I want to assign all values in a specific dimension to another array declared as follows:
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
27
by: the other john | last post by:
Is there a way or a property that can tell me how many items are in an array? Like when using the Split function but the actual number of created array items is unknown? I need to be able to...
4
by: jimgardener | last post by:
hi, (i posted this to numpy discussion grp couple of days back ..but it fails to appear..)since it is needed for my work i would appreciate if anyone can help me with this question i have two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.