【问题标题】:How to change the array key numeric value to a different value如何将数组键数值更改为不同的值
【发布时间】:2011-09-12 02:42:08
【问题描述】:

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

下面是我收到的数据,

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

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

非常感谢任何帮助。

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    有点笨拙,可能有一些更简洁的东西,但这应该可以完成工作。

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

    【讨论】:

      【解决方案2】:

      我可能会写一个转换函数,所以像(没有测试 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' 
      ) ) ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多