ID: 25489
User updated by: postings-php-bug at hans-spath dot de
Reported By: postings-php-bug at hans-spath dot de
Status: Open
Bug Type: Documentation problem
Operating System: Irrelevant
PHP Version: 4.3.3
New Comment:
The last 2 tests in my example code do not show the problem, to fix
this, replace '$a[$x] = $y' by '$input[$x] = $y' and add var_dump(
$input ); after var_dump( $z );.
Previous Comments:
------------------------------------------------------------------------
[2003-09-11 07:55:53] postings-php-bug at hans-spath dot de
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"
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=25489&edit=1