0

I looked for an good answer but found none.

An API returns JSON like this [{id:31, name:"blue"},{id:22, name:"green"},..]

I would like to like to get the name from id $api->[where id= 2]

The only way I found was to convert this array to obj and having the id as key. Is there no better PHP way?

DoctorArnar
  • 160
  • 1
  • 8

4 Answers4

4

Your solution is the best way. Something like:

$results = json_decode($returned_JSON_string);

foreach($results as $result) {
    if($result->id == 2) {
        // Match!
        break;
    }
}

If you don't like the default behavior of json_decode creating an object, you can instruct it to create an array like this:

 $results = json_decode($returned_JSON_string, true);
jterry
  • 6,209
  • 2
  • 30
  • 36
  • Is this okay? ( it works but I had to add 'id' infront of numbers to do so. I'm not that pro in PHP. ) `$Obj = new stdClass(); foreach ($jobs as $item) { $key = 'id'.$item['id']; $Obj->$key = $item; } print_r($Obj->id12);` – DoctorArnar Jun 06 '13 at 18:39
  • It would work if you absolutely knew that ID `12` existed, but I'd go with the safer approach and loop through as I suggested. That way, you can just execute whatever you need to do with a particular ID (`2`, in my example, `12` in yours) where I commented `// Match!`. – jterry Jun 06 '13 at 18:47
2

I think you can just encode your json as associative array

$arr = json_decode($json, true); //note true as second parameter because we want as associative array

and now you can call it with

echo $arr[1]['id'];

or loop in the array.

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • That only works if the id's are 1,2,3? I fixed the example to be more clear, the id's are very random it would be wonderful if I can say that id should be the key for the associative array – DoctorArnar Jun 06 '13 at 17:39
2

You could use array_filter:

$results = json_decode($returned_JSON_string);
$item = array_filter ($results, function($item) { $item['id'] == 2; });
dave
  • 62,300
  • 5
  • 72
  • 93
  • (This is not a comment on the correctness of dave's answer or the appropriateness of it, it's a fine answer.) I always find these anonymous-function solutions hard to read. array_filter, array_map, array_reduce are all hard to figure out what is happening from a quick read. jterry's (simple) answer is much easier to read. It's probably because I'm not as comfortable with functional programming, but so be it. – matt Jun 06 '13 at 18:23
1

Here is the solution for doing it in javascript

function myIndexOf(arr, o) {    
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].id == o) {
                return i;
            }
        }
        return -1;
    }
    var data = [{id:1, name:"blue"},{id:2, name:"green"}];

    alert(data[myIndexOf(data,2)].name);

PHP version

    function myIndexOf($arr, o) {    
            for (var i = 0; i < count(arr); i++) {
                if ($arr[i]['id'] == o) {
                    return i;
                }
            }
            return -1;
        }

$data = json_decode($json_data);
echo $data[myIndexOf($data, 2)]['name']; // will output 'green'
Abdul Haseeb
  • 548
  • 4
  • 14