0

Json

{
    "articles": [
        {
            "id": 1,
            "name": "Josh" 
        },
        {
            "id": 2,
            "name": "Jenny"
        },
        {
            "id": 3,
            "name": "Chris"
        }
    ]
}

How do i search names by id?
What should I do if i want select only Josh?

Now I'm decoding json with php and foreach loop.

$url = "articles.json";
$json = json_decode(file_get_contents($url));
foreach($json->articles as $articles){
    echo $articles->name;
}

I want select only that name where id is 1.

1
  • You would have to do a forloop. If you want to search through a kind of query, you'll need to use a framework like jql, jsonpath, or a bigger framework that has json querying inside. Commented Jun 29, 2016 at 22:59

4 Answers 4

3
$json = '{
    "articles": [
        {
            "id": 1,
            "name": "Josh" 
        },
        {
            "id": 2,
            "name": "Jenny"
        },
        {
            "id": 3,
            "name": "Chris"
        }
    ]
}';

$json = json_decode($json);
$id = '1';
$result = 'NOT FOUND';
foreach($json->articles as $articles){
    if($articles->id == $id){
        $result = $articles;
    }
}

print_r($result);

output:

stdClass Object ( [id] => 1 [name] => Josh )

That way you keep both the id and the name tied.

Access it normaly, like so: echo $result->name;

Sign up to request clarification or add additional context in comments.

Comments

2

You can use array_filter to get every item in the array with the id you want.

$id = 1;  // define your id

// filter for that id
$results = array_filter($json->articles, function($x) use ($id) {
    return $x->id == $id;
});

The result will be an array with zero or more elements, depending on how many times the id is found. I'm not sure what you need to do with the results, but you can loop over them with foreach, or access specific elements by key like this:

echo $results[0]->name;   // echo the name of the first match

Comments

1
    $url = "articles.json";
    $json = json_decode(file_get_contents($url));
    foreach($json->articles as $articles){
        if($articles->id==1){
           echo "found id equal to 1";
    }
        if($articles->name=="Josh"){
           echo "found Josh";
    }
    }

Even better as a small function for fieldValue (id or name) with url, checkValue as additional parameters

   Function loopThruJSON ($json,$fieldValue,$checkValue) {
        $json = json_decode($json);
        foreach($json->articles as $articles){
            if($articles->{$fieldValue}==$checkValue){
               return "found ".$fieldValue." equal to ".$checkValue;
            } else {
               return false;
            }
        }
    }

Based on FirstOne's comment + example the above can be written

    Function loopThruJSON ($json,$fieldValue,$checkValue) {
            $json = json_decode($json);
            foreach($json->articles as $articles){
                if($articles->$fieldValue==$checkValue){
                   return "found ".$fieldValue." equal to ".$checkValue;
                } else {
                   return false;
                }
            }
     }
// Usage
echo loopThruJSONID ("articles.json",'id',1) ;
echo loopThruJSONName ("articles.json",'name',"Josh") ;

PHP sandbox example, 3v4l example

3 Comments

That won't work (the function part)... by the time you call the function, there is no $articles->id...
@FirstOne you are right what on earth am i writing :) one moment
@FirstOne deleted that please edit the answer accordingly thanks. :)
1

That's pretty easy:

function findById($articles, $id){
    foreach($articles as $article){
        if($article['id'] === $id)
           return $article;
    }
}

In this answer, you will need to pass true as a second parameter of json_decode() in order convert the data into associative arrays.

$json = json_decode(file_get_contents($url), true);

2 Comments

A side note, you call also use it as object (without the true in the json_decode) by changing from $article['id'] to $article->id.
@FirstOne Yea I know, but I find the other one easier to read, I dont like that one dunno why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.