2

I have array :

Array
(
    [0] => 166
    [role_code] => new role test
)

I want to change [0] into [menu_id]. How i can change it on PHP code?

2 Answers 2

4

You could use the union operator and then unset index 0:

$array = [ 'menu_id' => $array[0] ] + $array;
unset($array[0]);
Sign up to request clarification or add additional context in comments.

Comments

2

You can just assign the value to a new item and unset the previous one.

$array['menu_id'] = $array[0];
unset($array[0]);

1 Comment

Do note that this would change the order of the array, menu_id would become the 2nd item in this case, while role_code would be the 1st.

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.