php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16269 array_splice produces an unexpected result
Submitted: 2002-03-25 15:34 UTC Modified: 2002-07-07 21:38 UTC
From: saschahofmann at gmx dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.1.2 OS: windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: saschahofmann at gmx dot de
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-25 15:36 UTC] saschahofmann at gmx dot de
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
 [2002-03-26 08:04 UTC] sander@php.net
Your foreach-loop is wrong. It should be 
foreach ($array1 as $key => $val) {

Does it work now?
 [2002-03-26 08:33 UTC] saschahofmann at gmx dot de
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 10:51 UTC] 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 12:53 UTC] saschahofmann at gmx dot 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 13:37 UTC] 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 14:14 UTC] saschahofmann at gmx dot 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-07-07 21:38 UTC] andrei@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 10:01:33 2025 UTC