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