2011-08-08 15 views
9

Mam tablicy JSONJak przeglądać Array JSON w PHP

{ 
    "people":[ 
    { 
     "id": "8080", 
     "content": "foo" 
    }, 
    { 
     "id": "8097", 
     "content": "bar" 
    } 
    ] 
} 

Jak wyszukiwać 8097 i uzyskać zawartość?

+0

mogą tworzyć pętlę przejść peope-> array id – Ibu

+1

Ile ludzie są reprezentowani? Jeśli jest wystarczająco mały, to jedna z poniższych pętli wyszukiwania może działać dobrze. Jeśli jest bardzo duży, możesz potrzebować czegoś innego. –

+0

Czy wpisy są zawsze w porządku rosnącym id? Jeśli tak, zbudowany wokół niego algorytm może stworzyć coś znacznie bardziej wydajnego niż zapętlenie każdego wpisu. –

Odpowiedz

17

Funkcja json_decode powinno pomóc:

$str = '{ 
    "people":[ 
    { 
     "id": "8080", 
     "content": "foo" 
    }, 
    { 
     "id": "8097", 
     "content": "bar" 
    } 
    ] 
}'; 

$json = json_decode($str); 
foreach($json->people as $item) 
{ 
    if($item->id == "8097") 
    { 
     echo $item->content; 
    } 
} 
15

json_decode() go i traktować jak każdy inny tablicy lub obiektu stdClass

$arr = json_decode('{ 
    "people":[ 
    { 
     "id": "8080", 
     "content": "foo" 
    }, 
    { 
     "id": "8097", 
     "content": "bar" 
    } 
    ] 
}',true); 

$results = array_filter($arr['people'], function($people) { 
    return $people['id'] == 8097; 
}); 


var_dump($results); 

/* 
array(1) { 
    [1]=> 
    array(2) { 
    ["id"]=> 
    string(4) "8097" 
    ["content"]=> 
    string(3) "bar" 
    } 
} 
*/ 
+1

Myślę, że masz argumenty dla [array_map] (http://php.net/manual/en/function.array-map.php) poza kolejnością. –

+0

Użyłem array_map zamiast array_filter. Naprawiono teraz. – Mchl

4

Jeśli masz dość małą liczbę „lud” obiektów, wtedy poprzednie odpowiedzi będą dla ciebie skuteczne. Biorąc pod uwagę, że twój przykład ma identyfikatory z zakresu 8000, podejrzewam, że przeglądanie każdego identyfikatora może nie być idealne. Więc tutaj jest inna metoda, która zbada znacznie mniej ludzi przed znalezieniem się słuszna (tak długo, jak ludzie są w porządku ID):

//start with JSON stored as a string in $jsonStr variable 
// pull sorted array from JSON 
$sortedArray = json_decode($jsonStr, true); 
$target = 8097; //this can be changed to any other ID you need to find 
$targetPerson = findContentByIndex($sortedArray, $target, 0, count($sortedArray)); 
if ($targetPerson == -1) //no match was found 
    echo "No Match Found"; 


function findContentByIndex($sortedArray, $target, $low, $high) { 
    //this is basically a binary search 

    if ($high < low) return -1; //match not found 
    $mid = $low + (($high-$low)/2) 
    if ($sortedArray[$mid]['id'] > $target) 
     //search the first half of the remaining objects 
     return findContentByIndex($sortedArray, $target, $low, $mid - 1); 
    else if ($sortedArray[$mid]['id'] < $target) 
     //search the second half of the remaining objects 
     return findContentByIndex($sortedArray, $target, $mid + 1, $high); 
    else 
     //match found! return it! 
     return $sortedArray[$mid]; 
}