0

I have 2 arrays and want to copy keys from first array to second array and keep values on second array:

Arrays:

Array ( [8] => yellow [9] => violet ) 
Array ( [0] => blue [1] => orange )

Desired output:

Array ( [8] => yellow [9] => violet ) 
Array ( [8] => blue [9] => orange )
3
  • 1
    If you want to change keys of an array, probably you want to look for a more clean and concrete way to store your data? Commented Jun 6, 2018 at 19:53
  • I answered, but as @NicoHaase states, it is rarely needed to have the same indexes and it may be how you created or extracted the array data. Commented Jun 6, 2018 at 19:54
  • thanks, your answer work perfect to me. Commented Jun 6, 2018 at 19:56

1 Answer 1

3

Get the keys and combine:

$array2 = array_combine(array_keys($array1), $array2);
Sign up to request clarification or add additional context in comments.

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.