ID: 25489
User updated by: postings-php-bug at hans-spath dot de
Reported By: postings-php-bug at hans-spath dot de
-Status: Bogus
+Status: Open
Bug Type: Documentation problem
Operating System: Irrelevant
-PHP Version: 4.3.3
+PHP Version: 4.3.6
New Comment:
"... they behave exactly as the original functions"
( ( (( BULLSHIT ALARM )) ) )
THEY DO NOT.
TEST VALUES:
|| $input = array(
|| '1st' => 'first element',
|| 2 => 'second element',
|| '3rd' => 'third element'
|| );
|| $x = 2;
|| $y = 'replaced element';
*** array_push($input, $x, $y)
*** vs.
*** array_splice($input, count($input), 0, array($x, $y))
<TEST> array_push($input, $x, $y)
$input => array (
'1st' => 'first element',
2 => 'second element',
'3rd' => 'third element',
3 => 2,
4 => 'replaced element',
)
<TEST> array_splice($input, count($input), 0, array($x, $y))
$input => array (
'1st' => 'first element',
0 => 'second element',
'3rd' => 'third element',
1 => 2,
2 => 'replaced element',
)
--- EQUAL / EQUIVALENT ?!?
- differing return value (irrelevant)
- renumbered array-keys
*** array_pop($input)
*** vs.
*** array_splice($input, -1)
<TEST>
$z => 'third element'
$input => array (
'1st' => 'first element',
2 => 'second element',
)
<TEST> array_splice($input, -1)
$z => array (
'3rd' => 'third element',
)
$input => array (
'1st' => 'first element',
0 => 'second element',
)
--- EQUAL / EQUIVALENT ?!?
- return value differs (plain value vs. single-element array)
- renumbered array-keys
*** array_shift($input)
*** vs.
*** array_splice($input, 0, 1)
<TEST> array_shift($input)
$z => 'first element'
$input => array (
0 => 'second element',
'3rd' => 'third element',
)
<TEST> array_splice($input, 0, 1)
$z => array (
'1st' => 'first element',
)
$input => array (
0 => 'second element',
'3rd' => 'third element',
)
--- EQUAL / EQUIVALENT ?!?
- return value differs (plain value vs. single-element array)
*** $a[$x] = $y
*** vs.
*** array_splice($input, $x, 1, $y)
<TEST> $input[$x] = $y
$z => 'replaced element'
$input => array (
'1st' => 'first element',
2 => 'replaced element',
'3rd' => 'third element',
)
<TEST> array_splice($input, $x, 1, $y)
$z => array (
'3rd' => 'third element',
)
$input => array (
'1st' => 'first element',
0 => 'second element',
1 => 'replaced element',
)
--- EQUAL / EQUIVALENT ?!?
- return value differs (new value vs. replaced value) (irrelevant)
- what the hell is $a?
- OVERWRITE OF WRONG ELEMENT (because $x is a positon for array_splice,
not a key)
- renumbered array-keys
Previous Comments:
------------------------------------------------------------------------
[2004-07-05 12:30:27] [EMAIL PROTECTED]
Uhm.. They say equivalents, not equal. However, when that equivalents
are used in a separate line, they behave exactly as the original
functions.
------------------------------------------------------------------------
[2003-09-11 10:16:45] postings-php-bug at hans-spath dot de
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 );.
------------------------------------------------------------------------
[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