php获取json数据所有的节点路径
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-27 10:02:24 浏览: 评论:0
这篇文章主要介绍了php获取json数据所有的节点路径的方法和实例,有需要的小伙伴可以参考下。
之前我们讲解过使用javascript获取json数据节点路径的问题,今天我们更进一步,讲解下php获取json数据所有的节点路径。
- <?php
- function iterTree($data) {
- $retData = array();
- $data = json_decode($data, true);
- if (!is_array($data) && emptyempty($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) && !emptyempty($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);
Tags: php获取json节点
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)