For data similar to this, if I have a variable nId, how can I traverse it and know its name. [& Nbsp; {& nbsp; "Id" & nbsp;: & nbsp; "t1sd", & nbsp; "Name" & nbsp ;: & nbsp; "xiaoxiao", & nbsp; "Namea" & nbsp;: & nbsp; similar to this data, if I have a variable nId, how to traverse and know his 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 get the name "user 1" through code? share it:
------ Solution --------------------
$ S = <TXT
[{
"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"
}]
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