1

I need to use two arrays - initial one, and the same array without the first element. For example:

public function foobar($a, $b, $c)
{
   $initial_array = get_defined_var();
   var_dump($initial_array); // ok
   $sliced_array = array_shift($initial_array);
   var_dump($sliced_array); // int(1) ???

   //initial array should be 'a' => $a, 'b' => $b, 'c' => $c
   // sliced array should be 'b' => $b, 'c' => $c
}

The problem is that sliced array seems to be some strange value, like int(1)...What's wrong here?

3 Answers 3

4

You can try with array_slice:

$sliced_array = array_slice($initial_array, 1);
Sign up to request clarification or add additional context in comments.

Comments

0

array_shift modifies the original array, and return the shifted element, which is the value of $a = 1 in this case.

3 Comments

you should explain how to achieve what they want also, else this answer is incomplete
@SterlingArcher, I think the OP just want to know why it happened
Yes, but who goes on Main and asks why something is wrong without wanting to know how to fix it?
0

array_shift modifies the array it receives by removing its first element (as opposed to returning a copy of the array it receives)

If you want a copy of initial_array with the first element removed, try this

public function foobar($a, $b, $c)
{
   $initial_array = get_defined_var();      
   $sliced_array = $initial_array; //copy the array
   array_shift($sliced_array); //remove 1st element
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.