2011-09-12 41 views
0

我正在尝试从json curl调用中返回给我的数据,并更改这些键以便我可以更精确地与数据库调用进行匹配。如何将数组键值数值更改为不同的值

下面是我接收回数据,

阵列([0] =>数组([0] =>数组([toolbar_id] => thematrix [名称] =>矩阵))[ 1] =>数组([0] =>数组([toolbar_id] => neonlights [名称] =>霓虹灯))

加粗的区域是我想改变以匹配的值的关键['toolbar_id'];

非常感谢您的帮助,

回答

1

一点点的闪避方式,可能会有一些更简洁的东西,但这应该可以完成这项工作。

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

我可能会写一个转换功能,所以像(不考虑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' 
)) ;