开发者

How to change the array key numeric value to a different value

开发者 https://www.devze.com 2023-04-04 17:10 出处:网络
I am trying to take the data that is returned to me from a json curl call, and alter the keys so I can match more precisely with a database call.

I am trying to take the data that is returned to me from a json curl call, and alter the keys so I can match more precisely with a database call.

Below is the data I receive back,

Array ( [0] => Array ( [0] => Array ( [toolbar_id] => thematrix [name] => Matrix ) ) [1] => Array ( [0] => Array ( [toolbar_id] => neonlights [name] => NEON Ligh开发者_C百科ts ) )

The bolded area is the the key I want to change to match the value of the ['toolbar_id'];

Any help is greatly appreciated.


Bit of a bodge way, there may be something a little more concise, but this should do the job.

$newArr = array();
foreach ($arrReturn AS $key => $item)
{
    $newArr[$item[0]['toolbar_id']] = $item;
}
$arrReturn = $newArr;
unset($newArr);


I'd probably write a conversion function, so something like (without tests for isset() and the like left as an exercise for the view :) ;

function convert ( $arr, $items ) {
   $ret = array () ;
   foreach ( $arr as $idx => $item )
      $ret[$items[$idx]] = $item ;
   return $ret ;
}

$new_array = convert ( $your_array_here, array ( 
   'toolbar_id', 'other_id', 'something_else' 
) ) ;
0

精彩评论

暂无评论...
验证码 换一张
取 消