之前我们讲解过使用javascript获取json数据节点路径的问题,今天我们更进一步,讲解下php获取json数据所有的节点路径
<?php
function itertree($data) {
$retdata = array();
$data = json_decode($data, true);
if (!is_array($data) && empty($data)) {
echo 'error !' ."n";
} else {
$queue = array();
foreach ($data as $field => $value) {
$queue[] = $field;
}
$head = 0;
$tail = count($queue);
while ($head < $tail) {
$field = $queue[$head++];
$path = explode("/", $field);
$tmpdata = &$data;
foreach ($path as $key => $ph) {
$tmpdata = &$tmpdata[$ph];
}
if (is_array($tmpdata) && !empty($tmpdata)) {
$newfield = $field;
foreach ($tmpdata as $curfield => $curvalue) {
$newfield = $field . '/' . $curfield;
$queue[$tail++] = $newfield;
}
} else {
$retdata[] = $field;
}
}
}
return $retdata;
}
//测试数据
$data = file_get_contents("http://restapi.ele.me/v1/restaurants?extras%5b%5d=food_activity&extras%5b%5d=restaurant_activity&extras%5b%5d=certification&fields%5b%5d=id&fields%5b%5d=name&fiel
ds%5b%5d=phone&fields%5b%5d=promotion_info&fields%5b%5d=name_for_url&fields%5b%5d=flavors&fields%5b%5d=is_time_ensure&fields%5b%5d=is_premium&fields%5b%5d=image_path&fields%5b%5d=rating&fie
lds%5b%5d=is_free_delivery&fields%5b%5d=minimum_order_amount&fields%5b%5d=order_lead_time&fields%5b%5d=is_support_invoice&fields%5b%5d=is_new&fields%5b%5d=is_third_party_delivery&fields%5b%
5d=is_in_book_time&fields%5b%5d=rating_count&fields%5b%5d=address&fields%5b%5d=month_sales&fields%5b%5d=delivery_fee&fields%5b%5d=minimum_free_delivery_amount&fields%5b%5d=minimum_order_des
cription&fields%5b%5d=minimum_invoice_amount&fields%5b%5d=opening_hours&fields%5b%5d=is_online_payment&fields%5b%5d=status&fields%5b%5d=supports&fields%5b%5d=in_delivery_area&geohash=wx4g07
j0w1v7&is_premium=0&limit=1000&offset=24&type=geohash");
$ret = itertree($data);
print_r($ret);
以上所述就是本文的全部内容了,希望大家能够喜欢。
评论列表:
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 1天前回复该评论