Home  >  Article  >  Backend Development  >  PHP gets all node paths of json data_PHP tutorial

PHP gets all node paths of json data_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:51818browse

php gets all the node paths of json data

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

 ?

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

43

44

45

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[]=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);

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 43 44 45
<🎜> <🎜> <🎜>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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000071.htmlTechArticlephp Get all node paths of json data Before we explained the problem of using javascript to get the node path of json data, today we Going one step further, explain all the ways to obtain json data in php...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn