PHP在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: { }
}
]
}

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

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