这里有一个json字符串,需要获取registratition_id
的所有父数组位置
例如。我希望最后的数组能够输出这样的东西:
[0] =>0
[1] =>1
[2] =>3
实际上我想得到包含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"
},
{"message_id":"0:1396175384218718%b91f4d1ff9fd7ecd"
},
{"registration_id":"APA91bEgLFvrc0lnXqX3C1euQohdHrv_wbxtGP86ezRzGWEVMQPpJjw1GMhGzfkI8Q34TU1KRts2j_-7CyU4ce6MlX5DB3umpXDGl-Ebmg53b44UKga79ee9Sal6gT_9rP3KIz9pDEUk2JVJsQmxiWXWoIfrYEAmFg",
"message_id":"0:1396175384219100%50b5570df9fd7ecd"
},
{"message_id":"0:1396175384219927%50b5570df9fd7ecd"
},
{"error":"InvalidRegistration"
}]
}';
答案 0 :(得分:1)
使用foreach
循环并检查registration_id isset
$obj = json_decode($json_raw);
$resultsWithRegID = array();
foreach($obj->results as $index=>$element){
if(isset($element->registration_id)){
$resultsWithRegID[] = $index;
}
}
答案 1 :(得分:1)
只是一个替代版本:
$array_raw = json_decode($json_raw, true);
$what = "registration_id";
$res = array_keys(array_filter($array_raw['results'], function($item) use ($what)
{
return isset($item[$what]);
}));
var_dump($res);
显然你可以:
isset($item["registration_id"])
array_keys
(整个部分)
array (size=3)
0 =>
array (size=2)
'registration_id' => 'APA91bEgLFvrc0lnXqX3C1euQohdHrv...'
'message_id' => ''
1 =>
array (size=2)
'registration_id' => 'APA91bEgLFvrc0lnXqX3C1euQohdHrv...'
'message_id' => '0:1396175384218155%50b5570df9fd7ecd'
3 =>
array (size=2)
'registration_id' => 'APA91bEgLFvrc0lnXqX3C1euQohdHrv...'
'message_id' => '0:1396175384219100%50b5570df9fd7ecd'
答案 2 :(得分:0)
将JSON转换为数组 - 其余是你的:
$array = json_decode($json_raw, true);
// results with registration_id
$results = array();
foreach($array['results'] as $index => $element) {
if(isset($element['registration_id']) === true) {
$results[] = $index;
}
}
var_dump($results);
答案 3 :(得分:0)
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
$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"
},
{"message_id":"0:1396175384218718%b91f4d1ff9fd7ecd"
},
{"registration_id":"APA91bEgLFvrc0lnXqX3C1euQohdHrv_wbxtGP86ezRzGWEVMQPpJjw1GMhGzfkI8Q34TU1KRts2j_-7CyU4ce6MlX5DB3umpXDGl-Ebmg53b44UKga79ee9Sal6gT_9rP3KIz9pDEUk2JVJsQmxiWXWoIfrYEAmFg",
"message_id":"0:1396175384219100%50b5570df9fd7ecd"
},
{"message_id":"0:1396175384219927%50b5570df9fd7ecd"
},
{"error":"InvalidRegistration"
}]
}';
$data=json_decode ($json_raw);
$arr= $data->{'results'};
$i=0;
$n_r=array();
foreach($arr as $d)
{$n_r[]=$arr[$i]->{'registration_id'};
$i=$i+1;}
print_r($n_r);
?>
</body>
</html>