这里 a 有一个 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"
}]
}';
使用 foreach
循环并检查是否registration_id isset
$obj = json_decode($json_raw);
$resultsWithRegID = array();
foreach($obj->results as $index=>$element){
if(isset($element->registration_id)){
$resultsWithRegID[] = $index;
}
}
只是一个替代版本:
$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'
<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>
将 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);