Nike Yulistia Angreni
Nike Yulistia Angreni

Reputation: 311

How To Change Specific Array Name In PHP

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?

Upvotes: 2

Views: 60

Answers (2)

anFfWuy7qbJWaPp6DSOR
anFfWuy7qbJWaPp6DSOR

Reputation: 305

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

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

Upvotes: 4

dilasha
dilasha

Reputation: 48

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

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

Upvotes: 2

Related Questions