Bug #16269 Updated: array_splice produces an unexpected result

From: Date: Tue, 26 Mar 2002 15:51:27 +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-3399@lists.php.net to get a copy of this message
ID: 16269 Updated by: sander@php.net Reported By: saschahofmann@gmx.de -Status: Open +Status: Feedback Bug Type: Arrays related Operating System: windows 2000 PHP Version: 4.1.2 New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [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 ------------------------------------------------------------------------ [2002-03-26 08:04:48] sander@php.net Your foreach-loop is wrong. It should be foreach ($array1 as $key => $val) { Does it work now? ------------------------------------------------------------------------ [2002-03-25 15:36:33] saschahofmann@gmx.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-25 15:34:16] saschahofmann@gmx.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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=16269&edit=1

« previous php.bugs (#3399) next »