|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2002-03-25 15:34 UTC] saschahofmann at gmx dot de
 Hi folks,
I'm using PHP 4.1.2 as module with apache 1.3.23
I have this array:
$array1[0] = array("a" => 3, "b" => 3);
$array1[1] = array("a" => 9, "b" => 3);
$array1[2] = array("a" => 11, "b" => 11);
$array1[3] = array("a" => 11, "b" => 11);
$array1[4] = array("a" => 15, "b" => 11);
$array1[5] = array("a" => 2, "b" => 2);
I want to split the array in to arrays with array1 containing all elements where a = b and array2 a <> b
The correct result should be
even: element 0,2,3,5
uneven: element 1,4
I used the following code to split the array:
foreach ($array as $key = $val) {
    if ($val["a"] == $val["b"]) {
        $array2[] = array_splice($array1,$key,1);
    }
}
$array1 should now contain all uneven elements and $array2 all even elements (with a different structur but that doesn't matters here).
But the result is:
$array1 contains element 1,3,4
$array2 contains 4 elements but one is empty! The data of element 3 are missing.
When I run the script with this comparison:
if ($val["a"] != $val["b"])
I got the correct numbers of elements in each array (2 and 4)
but with wrong values.
I think this is a bug. Please inform me if you have investigated the issue and found a explanation (maybe I did something wrong).
best,
Sascha
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 09:00:01 2025 UTC | 
foreach ($array1 as $key = $val) { if ($val["a"] == $val["b"]) { $array2[] = array_splice($array1,$key,1); } } is the correct code. I forgot the '1' in the first variableYour foreach-loop is wrong. It should be foreach ($array1 as $key => $val) { Does it work now?That 1 is not what I meant, I was talking about => instead of =. You are using foreach ($array1 as $key = $val) { and it should be foreach ($array1 as $key => $val) { Does it work now?Oh oh, I'm very sorry. I should be more accurated when writing a message :-) Here is the code again (by copy & paste): <?php $array1[0] = array("a" => 3, "b" => 3); $array1[1] = array("a" => 9, "b" => 3); $array1[2] = array("a" => 11, "b" => 11); $array1[3] = array("a" => 11, "b" => 11); $array1[4] = array("a" => 15, "b" => 11); $array1[5] = array("a" => 2, "b" => 2); foreach ($array1 as $key => $val) { if ($val["a"] == $val["b"]) { $array2[] = array_splice($array1,$key,1); } } echo "array1:<br>"; echo "<pre>"; var_dump($array1); echo "</pre>"; echo "<br>array2:<br>"; echo "<pre>"; var_dump($array2); echo "</pre>"; ?> Please, try it out. You'll get a weird result...I suspected this as a possible source for the wrong result and already tried it out. Here is the modified code again: <?php $array1[0] = array("a" => 3, "b" => 3); $array1[1] = array("a" => 9, "b" => 3); $array1[2] = array("a" => 11, "b" => 11); $array1[3] = array("a" => 11, "b" => 11); $array1[4] = array("a" => 15, "b" => 11); $array1[5] = array("a" => 2, "b" => 2); $array_backup = $array1; foreach ($array1 as $key => $val) { if ($val["a"] == $val["b"]) { $array2[] = array_splice($array_backup,$key,1); } } echo "array_backup:<br>"; echo "<pre>"; var_dump($array_backup); echo "</pre>"; echo "<br>array2:<br>"; echo "<pre>"; var_dump($array2); echo "</pre>"; ?> To be sure I even loaded $array_backup manually to exclude the unlikely case of referencing the variable instead of copying. In my special case the curious behaviour of the function is not that important. I find another way to solve the problem. But I thought it is a good idea to inform you and your developing team. I think it is a bug.