-1

i have a json file of users with all descriptions and its hard to find them with data[2]. imagine in data[0]->users->data i have 3 user and i want to find name = "Stafin" to get description->data->instagram and get value. i'll give you a simple json data

{"data":[
  {
     "id":1,
     "category_name":"Writers",
     "users":{
        "data":[{
          "name":"Steve",
          "id":"1",
          "description":{
             "data":[
                {
                   "instagram":"steveid"
                }
             ]
          }
       },{
          "name":"Stafin",
          "id":"2",
          "description":{
             "data":[
                {
                   "instagram":"stafinid"
                }
             ]
          }
       },{
          "name":"Sara",
          "id":"3",
          "description":{
             "data":[
                {
                   "instagram":"saraid"
                }
             ]
          }
       }]
     }
  }
]}

Code:

<?php
    $str = file_get_contents('http://localhost/json/test.json');
    $json = json_decode($str, true);

    function searchForId($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['id'] === $id) {
               return $key;
           }
       }
       return null;
    }
    $id = searchForId('2', $json);
    echo $id;
?>

note that: answer it in php language sorry for my bad English language. if you didn't get that, just tell me to describe more

Ali
  • 91
  • 2
  • 9

1 Answers1

-1

Your function searches for id, but you said you wanted to search for name. You can make the key another parameter of the function. Then you can use it to find the id in the data array, and the name in the users['data'] array.

function searchArray($searchkey, $searchval, $array) {
   foreach ($array as $key => $val) {
       if ($val[$searchkey] === $searchval) {
           return $val;
       }
   }
   return null;
}

foreach ($json['data'] as $data) {
    $user = searchArray('name', 'Stafin', $data['users']['data']);
    if ($user) {
        echo "Found Stafin";
        foreach ($user['description']['data'] as $desc) {
            if (isset($desc['instagram'])) {
                echo ", Instagram is {$desc['instagram']}";
                break;
            }
        }
        break;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • tnx, but i think you missed something: `Parse error: syntax error, unexpected '}', expecting ';' in C:\xampp\htdocs\json\index.php on line 18` – Ali Jan 23 '20 at 11:07
  • I forgot the `;` on the return line – Barmar Jan 23 '20 at 11:09
  • :| `Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\json\index.php on line 12` – Ali Jan 23 '20 at 11:11
  • There's no id 2 in your data. I changed it to id 1. – Barmar Jan 23 '20 at 11:26
  • please just test your code. `Notice: Undefined index: name in C:\xampp\htdocs\json\index.php on line 13`. this is the line 13: `if ($val[$searchkey] === $searchval) {` – Ali Jan 23 '20 at 11:37
  • Will my problem be solved if I cry? – Ali Jan 23 '20 at 11:39
  • Sorry, it should be `$data['users']['data']`. – Barmar Jan 23 '20 at 11:41
  • I'm just going by what you wrote in the question. You said to look in `data[0]->users->data`. `data[0]` is what you get when you search for `id = 1` in `$data`. Then I search for `name = 'Staffin` in `users->data`. – Barmar Jan 23 '20 at 12:00
  • You could just use nested loops to search all the top-level arrays for `user = Staffin` in the inner arrays. – Barmar Jan 23 '20 at 12:00
  • I've rewritten it to show the nested loops. – Barmar Jan 23 '20 at 12:05
  • It's not tested. If it doesn't work, try debugging it yourself and let me know what I missed. – Barmar Jan 23 '20 at 12:06
  • it's working just fine. Thank you for your patience with a newbie coder :) – Ali Jan 23 '20 at 12:11