How can I get all the positions in the array results
where the key registration_id
exists ?
如何获得数组结果中键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"
}]
}';
Thanks for any help!
感谢任何帮助!
1 个解决方案
#1
1
You can use json_decode()
for this.
您可以为此使用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
}
}
Or if you want to only keep the ones where registration_id
exists, use array_filter()
:
或者如果您只想保留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");
#1
1
You can use json_decode()
for this.
您可以为此使用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
}
}
Or if you want to only keep the ones where registration_id
exists, use array_filter()
:
或者如果您只想保留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");