Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP获取json数组键位置_Php_Json_Multidimensional Array - Fatal编程技术网

PHP获取json数组键位置

PHP获取json数组键位置,php,json,multidimensional-array,Php,Json,Multidimensional Array,如何获取数组results中存在密钥registration\u id的所有位置 $json_raw = '{"multicast_id":6446899316497614986, "success":5, "failure":1, "canonical_ids":3, "results":[

如何获取数组
results
中存在密钥
registration\u id
的所有位置

  $json_raw = '{"multicast_id":6446899316497614986,
                     "success":5,
                     "failure":1,
                     "canonical_ids":3,
                     "results":[
                             {"registration_id":"APA91bEgLFvrc0lnXqX3C1euQohdHrv_wbxtGP86ezRzGWEVMQPpJjw1GMhGzfkI8Q34TU1KRts2j_-7CyU4ce6MlX5DB3umpXDGl-Ebmg53b44UKga79ee9Sal6gT_9rP3KIz9pDEUk2JVJsQmxiWXWoIfrYEAmFg",
                               "message_id":"0:1396175384218906%50b5570df9fd7ecd"
                             },
                             {"registration_id":"APA91bEgLFvrc0lnXqX3C1euQohdHrv_wbxtGP86ezRzGWEVMQPpJjw1GMhGzfkI8Q34TU1KRts2j_-7CyU4ce6MlX5DB3umpXDGl-Ebmg53b44UKga79ee9Sal6gT_9rP3KIz9pDEUk2JVJsQmxiWXWoIfrYEAmFg",
                              "message_id":"0:1396175384218155%50b5570df9fd7ecd"
                             },
                             {"registration_id":"APA91bEgLFvrc0lnXqX3C1euQohdHrv_wbxtGP86ezRzGWEVMQPpJjw1GMhGzfkI8Q34TU1KRts2j_-7CyU4ce6MlX5DB3umpXDGl-Ebmg53b44UKga79ee9Sal6gT_9rP3KIz9pDEUk2JVJsQmxiWXWoIfrYEAmFg",
                              "message_id":"0:1396175384219100%50b5570df9fd7ecd"
                             },
                             {"message_id":"0:1396175384218718%b91f4d1ff9fd7ecd"
                             },
                             {"message_id":"0:1396175384219927%50b5570df9fd7ecd"
                             },
                             {"error":"InvalidRegistration"
                             }]
                       }';
谢谢你的帮助

您可以使用它

$data = json_decode($json_raw, TRUE);
foreach ($data['results'] as $key=>$result) {
    if (array_key_exists('registration_id', $result)) {
       //use now via $data['results'][$key] or simply $result
    }
}
或者,如果您只想保留
注册id
存在的位置,请使用:


我建议用户
array\u key\u exists
而不是
isSet
,因为如果密钥存在但其值为
null
,则后者将返回
false
——感谢这个伟大的答案,但我需要获得注册id的完整结果数组中的位置,而不是该密钥的值
function registrationIdExists($result) {
    return array_key_exists('registration_id', $result);
}

$data = json_decode($json_raw, TRUE);
$data['results'] = array_filter($data['results'], "registrationIdExists");