如何获得数组results
中键registration_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"
}]
}';
谢谢你的帮助!
您可以使用json_decode()
。
$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
}
}
或者如果您只想保留registration_id
存在的位置,请使用array_filter()
:
function registrationIdExists($result) {
return array_key_exists('registration_id', $result);
}
$data = json_decode($json_raw, TRUE);
$data['results'] = array_filter($data['results'], "registrationIdExists");