如何在不知道节点号的情况下从PHP中的JSON数组获取值?

时间:2014-09-11 10:53:44

标签: php arrays json

我正在使用各种权限接收下面的JSON数组。 如果您不知道它所在的节点,如何检查publish_actions [status]值? (目前它在[3]中,但可能是任何其他数字)

JSON数组:

Array (
    [data] => Array (
        [0] => Array (
            [permission] => installed
            [status] => granted
        )
        [1] => Array (
            [permission] => public_profile
            [status] => granted
        )
        [2] => Array (
            [permission] => manage_pages
            [status] => granted
        )
        [3] => Array (
            [permission] => publish_actions
            [status] => granted
        )
        [4] => Array (
            [permission] => user_groups
            [status] => granted
        )
    )
)

2 个答案:

答案 0 :(得分:2)

使用json_decode然后迭代数组:

$publish_actions_status = '';
$array=json_decode($json_string);
foreach($array['data'] as $node){
    if($node['permission']=='publish_actions'){
        $publish_actions_status = $node['status'];
        break;
    }
}

答案 1 :(得分:0)

尝试

foreach($data as $k=>$v) {
  if($v['permission']=='publish_actions'){
    $newarr[]['permission'] = $v['permission'];
    $newarr[]['status'] = $v['status']
  }
}
print_r($newarr);