ID:               16269
 Updated by:       [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
-Status:           Open
+Status:           Analyzed
 Bug Type:         Arrays related
 Operating System: windows 2000
 PHP Version:      4.1.2
 New Comment:

The problem is you're modifying the array while you're walking through
it with foreach. That causes the mess. Don't know what to do with this,
though.


Previous Comments:
------------------------------------------------------------------------

[2002-03-26 12:53:19] [EMAIL PROTECTED]

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...

------------------------------------------------------------------------

[2002-03-26 10:51:27] [EMAIL PROTECTED]

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?

------------------------------------------------------------------------

[2002-03-26 08:33:30] [EMAIL PROTECTED]

No, that wasn't the reason. I only misspelled the foreach-loop 
when I wrote the bug report (forgot the '1').
Sascha

------------------------------------------------------------------------

[2002-03-26 08:04:48] [EMAIL PROTECTED]

Your foreach-loop is wrong. It should be 
foreach ($array1 as $key => $val) {

Does it work now?

------------------------------------------------------------------------

[2002-03-25 15:36:33] [EMAIL PROTECTED]

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 variable

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/16269

-- 
Edit this bug report at http://bugs.php.net/?id=16269&edit=1

Reply via email to