PHP Encuentra todos los artículos en JSON

here is the original json which i want to search to get all occurrences of a specifiv value of a specific key

{services:
[
{
servicetypeid: "26",
serviceid: "50",
servicename: "Tax Evasion",
description: "Tax Evasion",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "27",
serviceid: "51",
servicename: "Parking",
description: "Parking Related Payments",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "27",
serviceid: "52",
servicename: "Markets",
description: "Markets Related Payments",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "27",
serviceid: "53",
servicename: "PSV",
description: "Public Service Vehicles",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "5",
serviceid: "54",
servicename: "Vehicle Bill",
description: "Check any Bill Attached to Your Vehicle",
optioncode: "212",
inputid: "216",
price: { },
categoryidentifier: { }
}
]
}

I've been trying to write a function like\ findInjson($jsonObj,$field,value)

that would return findInjson($jsonObj,'servicetypeid','27');

{services:[
{
servicetypeid: "27",
serviceid: "51",
servicename: "Parking",
description: "Parking Related Payments",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "27",
serviceid: "52",
servicename: "Markets",
description: "Markets Related Payments",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
},
{
servicetypeid: "27",
serviceid: "53",
servicename: "PSV",
description: "Public Service Vehicles",
optioncode: { },
inputid: { },
price: { },
categoryidentifier: { }
}
]
}

preguntado el 31 de julio de 12 a las 11:07

Will it allways be inside an array two times? (I mean [[{objectToSearch}]] -

just corrected it ... sorry about that -

1 Respuestas

To search by those keys in that array, you could do the following:

Solución para PHP 5.3

function findinjson($jsonContent, $field, $value)
{
    $jsonObject = json_decode($jsonContent, true);
    // This is to search inside the first array, which only has one element
    $searchArray = $jsonObject['services'];

    // Array filter applies the function given as a parameter to each element of $searchArray
    $filteredArray = array_filter($searchArray, function($element) use ($field, $value) {
        return array_key_exists($field, $element) && $element[$field] == $value;
    });

    // Output the data the same way it was given, but filtered.
    return json_encode(array('services' => $filteredArray));
}

Hope this is enough. If you need a PHP 5.2 solution you can, instead of using array filter, build $filteredArray by iterating $searchArray and only adding to $filteredArray the elements which pass the condition. Thus, the call to array_filter would be replaced by the following code:

$filteredArray = array();

foreach ($searchArray as $element) {
    if (array_key_exists($field, $element) && $element[$field] == $value) {
        $filteredArray[] = $element;
    }
}

Respondido 31 Jul 12, 13:07

getting a unexpected T_DOUBLE_ARROW on code return json_encode('services' => array($filteredArray)); - kmarima

Fixed it. Is everything ok now? - Lumbendil

Not sure what I'm doing wrong here ... getting this error Cannot use object of type stdClass as array in 'code' $searchArray = $jsonObject['services']; - kmarima

Sorry, my fault, forgot that by default json_decode returns an object. Setting it's second parameter to true fixes it (answer has been fixed). - Lumbendil

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.