php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #25489 wrong array_splice() equivalents given
Submitted: 2003-09-11 07:55 UTC Modified: 2004-09-16 15:00 UTC
From: postings-php-bug at hans-spath dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.6 OS: Irrelevant
Private report: No CVE-ID: None
 [2003-09-11 07:55 UTC] 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"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-11 10:16 UTC] 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 );.
 [2004-07-05 12:30 UTC] nlopess@php.net
Uhm.. They say equivalents, not equal. However, when that equivalents are used in a separate line, they behave exactly as the original functions.
 [2004-07-05 18:04 UTC] postings-php-bug at hans-spath dot de
"... 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
 [2004-09-16 15:00 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I changed "The following equivalences hold" to "The following statements change the values of $input the same way"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 05 22:01:26 2024 UTC