For data similar to this, if I have a variable nId, how can I traverse it and know its name.

Source: Internet
Author: User
For data similar to this, if I have a variable nId, how can I traverse it and know its name. [{
"Id": "t1sd ",
"Name": "xiaoxiao ",
"Namea": "Test appearance ",
"NId": "csd2"
},{
"Id": "ae2 ",
"Name": "Zhang San ",
"Namea": "Haha ",
"NId": "20af"
},{
"Id": "c3P ",
"Name": "User 1 ",
"Namea": "dingding ",
"NId": "4fc"
..............
}]

I have a set of data like this and I don't know the specific length of it. so I used "..." in it. .
If I know a variable nId, how do I get the value of its name.
For example, if I find an nId equal to 4fc. How can I obtain the name "user 1" through code ";


Reply to discussion (solution)

What is the data format?

If you only have such data, you can only traverse it.
Foreach ($ arr as $ data ){
If ($ data ['nid'] = '4fc '){
Echo $ data ['name'];
Break;
}
}
If the number of queries is large and the data volume is large, we recommend that you create an index first.
$ NId_array = array ();
Foreach ($ arr as $ data ){
$ NId_array [$ data ['nid'] = $ data ['name'];
}

Then, you can find the $ nId_array in each query.

$ S = <TXT [{"Id": "t1sd", "Name": "xiaoxiao", "Namea": "Test appearance", "nId ": "csd2" },{ "Id": "ae2", "Name": "Zhangsan", "Namea": "Haha", "nId": "20af "}, {"Id": "c3P", "Name": "User 1", "Namea": "dingding", "nId": "4fc"}] TXT; $ nId = '4fc '; $ t = json_decode ($ s, true); $ r = array_filter ($ t, function ($ a) use ($ nId) {return $ a ['nid'] ==$ nId;}); echo current ($ r) ['name'];
User 1

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.