2011-08-08 11 views
9

bir JSON Array arama yapmak Ben bir JSON dizisi Ben 8097 için arama ve içerik alacağı NasılNasıl PHP

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

?

+0

kaç peope-> id dizisi – Ibu

+1

geçmesi için bir döngü oluşturabilirsiniz: Yani burada (ID sırasına göre sürece insanlar gibi) doğru bir bulgu önce çok az kişi inceleyeceğiz diğer bir yöntemdir insanlar temsil edilir? Yeterince küçükse, aşağıda sunulan arama döngülerinden biri iyi çalışabilir. Çok büyükse, başka bir şeye ihtiyacınız olabilir. –

+0

Ayrıca, girişler her zaman artan sırayla mı? Eğer öyleyse, etrafında inşa edilmiş bir algoritma, her girişten döngülenmekten çok daha verimli bir şey üretebilir. –

cevap

17

başka dizi veya stdClass nesne gibi davranmak: Eğer "insan" nesnelerin oldukça az sayıda varsa

$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() onu ve json_decode fonksiyon size yardımcı olmalıdır

$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

Sanırım [array_map] (http://php.net/manual/en/function.array-map.php) için sıra dışı argümanlar var. –

+0

array_filter yerine array_map kullandım. Şimdi sabit. – Mchl

4

, Daha sonra önceki cevaplar sizin için çalışacaktır. Örneğinizin 8000 aralığındaki kimlikleri olduğu düşünüldüğünde, her bir kimliğin ideal olmayabileceğinden şüpheleniyorum.

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