dsaob80228 2012-07-31 10:56
浏览 41
已采纳

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: { }
}
]
}
  • 写回答

1条回答 默认 最新

  • dongzanxun2790 2012-07-31 11:12
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名