0

I want to shift an array by N from O, I mean, I want to insert N new values from O offset, and preserve the original array.

I took a look to array_splice, and in the example of php doc, they do what I want but it doesn't work for me. Here is my code :

function arrayShift(array $array, int $offset, int $length) {
    $insert = [];
    // create new array of $length size
    for ($i = 0; $i < $length; $i++) {
        $insert []= '0';
    }

    return array_splice($array, $offset, 0, $insert);
}

// my array is multidimensionnal with 16 values
$array = [
   0 => [
      'value1' => 'test',
   ],
   .....
];
var_dump(arrayShift($array, 5, 2));
// it returns empty array

I want this array :

array:10 [
  0 => array:3 [
    "type" => "choices"
    "choices" => array:1 []
    "field" => array:3 []
  ]
  1 => array:3 []
  2 => array:3 []
  3 => array:3 []
  4 => array:3 []
  5 => array:3 []
  6 => array:3 []
  7 => array:3 []
  8 => array:3 []
  9 => array:3 []
]

To become this array :

array:12 [
  0 => array:3 [
    "type" => "choices"
    "choices" => array:1 []
    "field" => array:3 []
  ]
  1 => array:3 []
  2 => array:3 []
  3 => array:3 []
  4 => array:3 []
  5 => array:3 []
  6 => '0',
  7 => '0',
  8 => array:3 []
  9 => array:3 []
  10 => array:3 []
  11 => array:3 []
]

Is it because it's a multidimensional array ? I don't think it changes something.

lospejos
  • 1,888
  • 3
  • 17
  • 31
Vincent Decaux
  • 7,375
  • 3
  • 37
  • 52
  • Can you share some sample data, expected output from given input? – Qirel Mar 15 '19 at 13:37
  • 2
    *[`array_splice`](http://php.net/array_splice) - **Returns an array consisting of the extracted elements.*** – deceze Mar 15 '19 at 13:40
  • 3
    Read the function signature carefully. `array_splice()` acts upon the original array by reference so the quickest solution to your issue could be `array_splice($array, $offset, 0, $insert); return $array;`. This of course assumes that the rest of your logic is correct. – MonkeyZeus Mar 15 '19 at 13:42
  • But look at their example, they add some elements into an array. And this answer helped me too : https://stackoverflow.com/a/3797526/999617 – Vincent Decaux Mar 15 '19 at 13:43
  • To make it even more plain: What `array_splice` ***returns*** will always be an empty array in your code, since you are not removing anything. You are not interested in its return value! – deceze Mar 15 '19 at 13:46
  • @MonkeyZeus oh thanks ! it was that ... Can you put it as an answer and I will accept it. I missreaded the documentation I guess – Vincent Decaux Mar 15 '19 at 13:47
  • Sure thing. I'll post 2 solutions in my answer so you can decide how to best proceed. – MonkeyZeus Mar 15 '19 at 13:49

2 Answers2

1

As denoted by the manual of array_splice(), the signature is

array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) : array

Note that the first argument is by reference (&$input). This means that after you call array_splice(), the changes will be made to the $input array, not to the result of the function-call.

The return-value of the function is "an array consisting of the extracted elements." (from the documentation). For you, that will mean an empty array - since you don't remove anything (the $length argument is always set to 0 in your code).

Therefor you should call the function, then return the array.

array_splice($array, $offset, 0, $insert);
return $array;

See the live demo.

Qirel
  • 21,424
  • 7
  • 36
  • 54
  • 1
    Your answer is right, but it was answered in comments, I prefer the author to get points – Vincent Decaux Mar 15 '19 at 13:57
  • Yeah, that's fair. I wasn't reading the comments, was typing up the answer and getting the sources. :-) the important thing is that you solved your issue! – Qirel Mar 15 '19 at 14:58
1

array_splice() acts upon the original array by reference per the function signature:

array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) : array
                     ^ the ampersand!

but it returns an array of values which you spliced out of the original array so to immediately fix your issue you would just change your function to:

array_splice($array, $offset, 0, $insert);
return $array;

If you want your function to behave similarly to array_splice() then you need to change your function signature into:

function arrayShift(array &$array, int $offset, int $length) {
    $insert = [];
    // create new array of $length size
    for ($i = 0; $i < $length; $i++) {
        $insert []= '0';
    }

    return array_splice($array, $offset, 0, $insert);
}

and you would use it like this:

$array = [
   0 => [
      'value1' => 'test',
   ],
   .....
];

arrayShift($array, 5, 2); // alter $array by reference
var_dump($array);
MonkeyZeus
  • 18,445
  • 3
  • 30
  • 67