Like this data, if I have a variable nid, how to traverse after going to know his name.

Source: Internet
Author: User
[ {
"Id": "T1SD",
"Name": "Xiaoxiao",
"Namea": "Test Look",
"NId": "CSD2"
}, {
"Id": "AE2",
"Name": "Zhang San",
"Namea": "Haha",
"NId": "20AF"
}, {
"Id": "c3p",
"Name": "User 1",
"Namea": "Dingding",
"NId": "4FC"
。。。。。。。。。。。。。。
} ]

I have such a group of data, and I do not know his specific length, so, it used me "... "To omit.
If I know a variable nid, how to go to the value of his name for the U-even.
For example, if I find a nid equals 4FC. How to get through the code to his name is "User 1";


Reply to discussion (solution)

What format is the data?

If you only have such a data, then you can only traverse it.
foreach ($arr as $data) {
if ($data [' nId ']== ' 4FC ') {
echo $data [' Name '];
Break
}
}
If the number of lookups is greater and the amount of data is large, it is recommended that you make an index first.
$nId _array = Array ();
foreach ($arr as $data) {
$nId _array [$data [' nId ']] = $data [' Name '];
}

And then every time you find it from this $nId _array.

$s =<<< txt[{  "id": "T1SD",  "Name": "Xiaoxiao", "  Namea": "Test Look",  "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
  • Related Article

    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.