Home > Article > Backend Development > PHP gets all node paths of json data_PHP tutorial
Previously we explained the problem of using javascript to get the json data node path. Today we go one step further and explain all the nodes of json data that php gets. Node path
?
|
<🎜> <🎜> <🎜>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; } //Test data $data = file_get_contents("http://restapi.ele.me/v1/restaurants?extras[]=food_activity&extras[]=restaura nt_activity&extras[]=certification&fields[]=id&fields[]=name&fiel ds[]=phone&fields[]=promotion_info&fields[]=name_for_url&fields[]=flavors&fields% 5B]=is_time_ensure&fields[]=is_premium&fields[]=image_path&fields[]=rating&fie lds[]=is_free_delivery&fields[]=minimum_order_amount&fields[]=order_lead_time&fields%5 B]=is_support_invoice&fields[]=is_new&fields[]=is_third_party_delivery&fields[% 5D=is_in_book_time&fields[]=rating_count&fields[]=address&fields[]=month_sales&fields% 5B]=delivery_fee&fields[]=minimum_free_delivery_amount&fields[]=minimum_order_des cription&fields[]=minimum_invoice_amount&fields[]=opening_hours&fields[]=is_online_pay ment&fields[]=status&fields[]=supports&fields[]=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 content of this article, I hope you all like it.