将multidimension数组键值获取到单个数组php

时间:2017-11-20 18:07:01

标签: php

{
    "list": [
        {
            "id": 62,
            "user_id": "34",
            "updated_at": "2017-10-15 08:24:32",
            "request": {
                "data": "yes",
                "watch": "Pending"
            }
        },
        {
            "id": 63,
            "user_id": "34",
            "updated_at": "2017-10-15 08:24:32",
            "request": {
                "data": "yes",
                "watch": "Yes"
            }
        },   ]
}

如何在update_at下获取监视值 如预期的那样

"list": [
        {
 "id": 62,
 "user_id": "34",
 "updated_at": "2017-10-15 08:24:32",
 "watch": "Pending"
}..

我尝试使用array_column但不适合我。

function array_column($array, $column){
            $ret = array();
            foreach($array as $row){
                $ret[] = $row[$column];
                return $ret;
            }
        }

3 个答案:

答案 0 :(得分:0)

有点像

function updateArray(&$array){
    foreach($array['list']  as $key => $item){
        $array['list'][$key]['watch'] = $item['request']['watch'];
        unset($array['list'][$key]['request']);
    }
}
updateArray($array);
print_r($array);

Demo

答案 1 :(得分:0)

我假设你知道你需要首先解码JSON:

$array = json_decode($json, true);

然后访问list元素下的数组:

$result = array_column($array['list'], 'updated_at');

答案 2 :(得分:0)

这是一个很好的挑战。你在哪里

 <pre>
<?php
$array=array("list"=>array(array("id"=>"62","user_id"=>"32","updated_at"=>2017,"request"=>array("data"=>"yes","watch"=>"panddin")),
                          array("id"=>"60","user_id"=>"30","updated_at"=>2016,"request"=>array("data"=>"no","watch"=>"pan"))));
//print_r($array);

            foreach($array as $row){
                foreach ($row as $key => $value){
                       if((is_array($value))){
                        foreach ($value as  $key1 => $k){
                              if((is_array($k))){
                              foreach ($k as $key2 => $value2) {
                                     echo $key2."=>". $value2."<br>";
                              }

                              }else echo  $key1."=>".$k."<br>";    

                              }

                       }else echo $key."=>".$value. "<br>"; 
                  }


            }
?>
 </pre>