473,289 Members | 1,940 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,289 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 2743
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.