PHP gets all node paths for JSON data

Source: Internet
Author: User
Tags foreach json

Before we talked about using JavaScript to get the JSON data node path, we went further today to explain all the node paths that PHP gets from the JSON data

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 <?php   function Itertree ($data) {$retData = array (); $data = Json_decode ($da TA, 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 = $f Ield; foreach ($tmpData as $curField => $curValue) {$newField = $field. '/' . $curField; $queue [$tail + +] = $newField; } else {$retData [] = $field;}} return $retData; }  //test Data $data = file_get_contents ("http://restapi.ele.me/v1/restaurants?extras%5b%5d=food_activity& Extras%5b%5d=restaura nt_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%5 B%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_pay ment&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);

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.