Php obtains all the node paths of json data.

Source: Internet
Author: User

Php obtains all the node paths of json data.

We have previously explained how to use javascript to obtain the Node path of json data. Today we will go further to explain how php obtains all the node paths of 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

43

44

45

<? Php

 

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_detail & 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 all the content of this article. I hope you will like it.

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.