|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2003-09-11 10:16 UTC] postings-php-bug at hans-spath dot de
[2004-07-05 12:30 UTC] nlopess@php.net
[2004-07-05 18:04 UTC] postings-php-bug at hans-spath dot de
[2004-09-16 15:00 UTC] vrana@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 11:00:02 2025 UTC |
Description: ------------ The documentation for array_splice gives some "equivalents" for special cases in Table 1. These are WRONG. array_pop and array_shift return single *VALUES*, array_splice returns an array. array_pop and array_shift do NOT perform the same action. In "$a[$x] = $y", $x is a KEY. In array_splice($input, $x, 1, $y), $x is a POSITION. Reproduce code: --------------- D:\PHP>cat test\array_splice.php <? $tests = array( 'array_shift($input)', 'array_pop($input)', 'array_splice($input, -1)', '$a[$x] = $y', 'array_splice($input, $x, 1, $y)' ); foreach( $tests as $test ) { $input = array( '1st' => 'first element', 2 => 'second element', '3rd' => 'third element' ); $x = 2; $y = 'replaced element'; echo "\n$test\n"; eval( "\$z = $test ;" ); var_dump( $z ); } Expected result: ---------------- *I* expect it to work as it works, but people misled by the documentation could expect something else. Actual result: -------------- D:\PHP>4.3.3\php-cli.exe -n test\array_splice.php array_shift($input) string(13) "first element" array_pop($input) string(13) "third element" array_splice($input, -1) array(1) { ["3rd"]=> string(13) "third element" } $a[$x] = $y string(16) "replaced element" array_splice($input, $x, 1, $y) array(1) { ["3rd"]=> string(13) "third element" }