2016-06-29 4 views
0

JsonPhp json Dekodierung mit mehreren Objekten

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

Wie kann ich Namen von id suchen?
Was soll ich tun, wenn ich nur Josh auswählen möchte?

Jetzt dekodiere ich Json mit PHP und foreach Schleife.

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

ich wählen möchte nur diesen Namen, wo id 1.

+0

Sie müssten eine forloop tun. Wenn Sie eine Art von Abfrage durchsuchen möchten, müssen Sie ein Framework wie jql, jsonpath oder ein größeres Framework verwenden, in dem json nachfragt. –

Antwort

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"; 
    } 
    } 

Noch besser als eine kleine Funktion für fieldValue (id oder name) mit url, checkValue als zusätzliche Parameter

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; 
      } 
     } 
    } 

Basierend auf FirstOne's Kommentar + Beispiel das oben genannte kann geschrieben werden

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

+0

Das wird nicht funktionieren (der Funktionsteil) ... bis Sie die Funktion aufrufen, gibt es keine "$ articles-> id" ... – FirstOne

+0

So etwas, vielleicht: [https://3v4l.org /UqE8n](https://3v4l.org/UqE8n) – FirstOne

+0

@FirstOne du bist richtig, was auf der Erde bin ich schreibe :) einen Moment –

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); 

Ausgabe lautet:

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

So halten Sie sowohl die id und die name gebunden.

Zugang es normaly, etwa so: echo $result->name;

1

Das recht einfach ist:

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

In dieser Antwort, müssen Sie true als zweiten Parameter von json_decode() konvertieren, um die Daten in assoziative Arrays zu übergeben.

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

_type 8 - Undefinierte Variable: article_. Du hast die foreach Bestellung falsch, d ... – FirstOne

+0

oops, ich habe seit einiger Zeit weg von php lol – meda

+0

Eine Randnotiz, Sie nennen es auch als Objekt (ohne die 'wahre' in der Json_decode) durch den Wechsel von '$ article ['id']' zu '$ article-> id'. – FirstOne

2

können Sie array_filter verwenden jedes Element im Array mit der ID Sie wollen zu bekommen.

$id = 1; // define your id 

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

Das Ergebnis ist ein Array mit null oder mehr Elementen, abhängig davon, wie oft die ID gefunden wird. Ich bin mir nicht sicher, was Sie mit den Ergebnissen tun müssen, aber man kann über sie Schleife mit foreach oder zugangsspezifische Elemente durch Schlüssel wie folgt aus:

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