Si usted tiene un número bastante reducido de objetos "pueblo", entonces las respuestas anteriores funcionarán para ti. Dado que su ejemplo tiene identificaciones en el rango de 8000, sospecho que mirar todas las identificaciones podría no ser ideal. Así que aquí es otro método que va a examinar un número mucho menor de personas antes de encontrar la correcta (siempre y cuando las personas están en el orden de Identificación):
//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];
}
puede crear un bucle para pasar por peope-> array Identificación – Ibu
¿Cuántas las personas están representadas? Si es suficientemente pequeño, entonces uno de los bucles de búsqueda presentados a continuación podría funcionar bien. Si es muy grande, es posible que necesite algo más. –
Además, ¿las entradas están siempre en orden creciente de id? Si es así, un algoritmo construido alrededor de eso podría producir algo mucho más eficiente que recorrer cada entrada. –