lockdown
lockdown

Reputation: 1496

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

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 Lights ) )

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

Any help is greatly appreciated.

Upvotes: 0

Views: 69

Answers (2)

AlexanderJohannesen
AlexanderJohannesen

Reputation: 2028

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' 
) ) ;

Upvotes: 0

Joe
Joe

Reputation: 15812

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);

Upvotes: 1

Related Questions