我正在尝试解析此 JSON 中的所有 no's 数组:
[{"page":1,"threads":[{"no":20783566,"last_modified":1417023255},{"no":20789075,"last_modified":1417023250},{"no":20777699,"last_modified":1417023250}]},{"page":2,"threads":[{"no":20753588,"last_modified":1417023105},{"no":20784845,"last_modified":1417023103},{"no":20789489,"last_modified":1417023099},{"no":20788012,"last_modified":1417023074}]}]
这是我到目前为止所拥有的:
$array = json_decode($rawjson, true);
$threads = array();
$threadids = array();
for ($i = 0; $i < count($array); ++$i) {
array_push($threads, $array[$i]['threads']);
}
for ($i = 0; $i < count($threads); ++$i) {
array_push($threadids, $threads[$i]['no']);
}
我知道第一个 for
语句不像它应该的那样工作,我需要array_push不要在原始数组中创建数组,但我不知道该怎么做。我无法让第二个 for
语句工作,因为我需要在每个数组内循环......
我想要的最终结果是一个仅包含no
值的数组。我不想要任何数组格式。如果有人知道更好的方法,请告诉我。
谢谢!
我想你想要的是...
$array = json_decode($rawjson, true);
$threads = array();
$threadids = array();
for ($i = 0; $i < count($array); ++$i) {
array_push($threads, $array[$i]['threads']);
}
for ($i = 0; $i < count($threads); ++$i) {
foreach ($threads[$i] as $thread) {
array_push($threadids, $thread['no']);
}
}
使用print_r查看数组的外观。 它会救你的命!
更好的是...(我个人在这些情况下更喜欢foreach)
$array = json_decode($rawjson, true);
$threads = array();
$threadids = array();
foreach ($array as $page) {
array_push($threads, $page['threads']);
}
foreach($threads as $temp) {
foreach ($temp as $thread) {
array_push($threadids, $thread['no']);
}
}