PHP gets all the node paths of JSON data _php tutorial

Source: Internet
Author: User

PHP gets all the node paths of the JSON data


Before we explained the problem of using JavaScript to get the JSON data node path, today we went further and explained that PHP gets all the node paths of the JSON data.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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%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 mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1000071.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000071.html techarticle PHP Get all the JSON data node path before we explained the use of JavaScript to get the JSON data node path problem, today we further, explain the next PHP get JSON data all ...

  • 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.