如何将数组键数值更改为其他值

时间:2022-10-26 13:40:27

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.

我试图从json curl调用中获取返回给我的数据,并更改键,以便我可以更精确地匹配数据库调用。

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

数组([0] =>数组([0] =>数组([toolbar_id] => thematrix [名称] =>矩阵))[1] =>数组([0] =>数组([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'];

粗体区域是我想要更改的键以匹配['toolbar_id']的值;

Any help is greatly appreciated.

任何帮助是极大的赞赏。

2 个解决方案

#1


1  

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

#2


0  

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

我可能会写一个转换函数,所以像(没有测试isset()之类的东西留作视图的练习:);

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

#1


1  

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

#2


0  

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

我可能会写一个转换函数,所以像(没有测试isset()之类的东西留作视图的练习:);

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