Is it just me or is array_splice acting like array_slice in PHP4.03?? I've hit my head for hours now wishing it was a simple typo of a missing "p".

    • [deleted]

    Well if you tell us something about what your problem is we might be able to help you :-)

      The problem:

      $arrayOne[] = "bla one";
      $arrayOne[] = "bla two";
      $arrayOne[] = "bla three";
      $arrayOne[] = "junk";
      $arrayOne[] = "bla five";

      $arrayTwo[] = "zaa four";

      // array_splice -- Remove a portion of the array and replace it
      // with something else
      // array array_splice (array input, int offset [, int length [, array replacement]])

      $perfectArray = array_splice($arrayOne, 3, 1, $arrayTwo);

      I would expect $perfectArray to be:

      $perfectArray[0] = "bla one";
      $perfectArray[1] = "bla two";
      $perfectArray[2] = "bla three";
      $perfectArray[3] = "zaa four";
      $perfectArray[4] = "bla five";

      But instead I get:

      $perfectArray[0] = "junk";

      This is what I would expect from array_slice not array_splice. This in PHP4.03. I've have a work around but these seems a little freaky... does anyone else encounter this and if so what version of PHP? Anyone know whare to post bugs?

      Dee

        • [deleted]

        And what is "junk" exactly?

          8 years later
          Anon;10119397 wrote:

          The problem:

          $arrayOne[] = "bla one";
          $arrayOne[] = "bla two";
          $arrayOne[] = "bla three";
          $arrayOne[] = "junk";
          $arrayOne[] = "bla five";

          $arrayTwo[] = "zaa four";

          // array_splice -- Remove a portion of the array and replace it
          // with something else
          // array array_splice (array input, int offset [, int length [, array replacement]])

          $perfectArray = array_splice($arrayOne, 3, 1, $arrayTwo);

          I would expect $perfectArray to be:

          $perfectArray[0] = "bla one";
          $perfectArray[1] = "bla two";
          $perfectArray[2] = "bla three";
          $perfectArray[3] = "zaa four";
          $perfectArray[4] = "bla five";

          But instead I get:

          $perfectArray[0] = "junk";

          This is what I would expect from array_slice not array_splice. This in PHP4.03. I've have a work around but these seems a little freaky... does anyone else encounter this and if so what version of PHP? Anyone know whare to post bugs?

          Dee

          Hi Anon,
          I got the same problem in my PHP 5.2.5 (cli) (built: Nov 8 2007 23:18:51).

          php -r "$input=array('red','green','blue','yellow'); print_r(array_splice($input,2));"

          It was suppose to give output array("red", "green")
          instead it gives me
          Array
          (
          [0] => blue
          [1] => yellow
          )

          php -r "$input=array('red','green','blue','yellow'); print_r(array_splice($input,2,1));"
          suppose to output Array('red','green','yellow');
          but it gives Array ( [0] => blue )

          I found array_splice is behaving same like array_slice in my php 🙁

          If anyone has any solution without changing the version of php please let me know, or post this bug.

            array_splice() returns the extracted elements as an array. Rather, you want to write:

            $perfectArray = $arrayOne;
            array_splice($perfectArray, 3, 1, $arrayTwo);

            EDIT:
            Incidentally, this thread is 7 years old or so, but I guess that since it was not resolved there is something to add to it after all.

              laserlight;10882741 wrote:

              array_splice() returns the extracted elements as an array. Rather, you want to write:

              $perfectArray = $arrayOne;
              array_splice($perfectArray, 3, 1, $arrayTwo);

              EDIT:
              Incidentally, this thread is 7 years old or so, but I guess that since it was not resolved there is something to add to it after all.

              laserlight,
              Thanks for your reply, I got it.

              $input=array('red','green','blue','yellow');
              print_r(array_splice($input,2,1));
              print_r($input); //This is what I need, Anon was also looking for this
              will output
              Array
              (
                  [0] => blue
              )
              Array
              (
                  [0] => red
                  [1] => green
                  [2] => yellow
              )
              

                No problem, I have marked this ancient thread as resolved.

                  Write a Reply...