Bug #16269 Updated: array_splice produces an unexpected result

From: Date: Mon, 08 Jul 2002 01:38:39 +0000
Subject: Bug #16269 Updated: array_splice produces an unexpected result
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-13458@lists.php.net to get a copy of this message
 ID:               16269
 Updated by:       andrei@php.net
 Reported By:      saschahofmann@gmx.de
-Status:           Analyzed
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: windows 2000
 PHP Version:      4.1.2
 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

You are modifying the array but using the keys from the original one.
Bogus.


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

[2002-03-26 14:14:23] saschahofmann@gmx.de

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.

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

[2002-03-26 13:37:33] sander@php.net

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] saschahofmann@gmx.de

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] sander@php.net

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] saschahofmann@gmx.de

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

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

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



Thread (9 messages)

« previous php.bugs (#13458) next »