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

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.


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

[2002-03-26 13:37:33] [EMAIL PROTECTED]

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.

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

[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?

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

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