Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP rename array keys in multidimensional array

Tags:

arrays

php

In an array such as the one below, how could I rename "fee_id" to "id"?

Array (     [0] => Array         (             [fee_id] => 15             [fee_amount] => 308.5             [year] => 2009                         )      [1] => Array         (             [fee_id] => 14             [fee_amount] => 308.5             [year] => 2009          )  ) 
like image 262
stef Avatar asked Feb 06 '10 11:02

stef


People also ask

How to rename array key in PHP?

$new = "new_name"; $array[$new]=$array["Order_no"]; unset($array["Order_no"]); print_r($array);

How do you change array keys?

Description ¶ array_replace() replaces the values of array with values having the same keys in each of the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array.


1 Answers

foreach ( $array as $k=>$v ) {   $array[$k] ['id'] = $array[$k] ['fee_id'];   unset($array[$k]['fee_id']); } 

This should work

like image 183
lamas Avatar answered Sep 19 '22 18:09

lamas