使用php过滤JSON数据

5

我正在尝试遍历这个JSON文件并过滤掉不需要的元素,我希望将结果分割,以便获得客户列表或供应商列表。

{
  "$descriptor": "Test",
  "$resources": [
    {
      "$uuid": "281d393c-7c32-4640-aca2-c286f6467bb1",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "87a132a9-95d3-47fd-8667-77cca9c78436",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "8a75345c-73c7-4852-865c-f468c93c8306",
      "$descriptor": "",
      "customerSupplierFlag": "Supplier"
    },
    {
      "$uuid": "a2705e38-18d8-4669-a2fb-f18a87cd1cc6",
      "$descriptor": "",
      "customerSupplierFlag": "Supplier"
    }
  ]
}

例如
    {
      "$uuid": "281d393c-7c32-4640-aca2-c286f6467bb1",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },
    {
      "$uuid": "87a132a9-95d3-47fd-8667-77cca9c78436",
      "$descriptor": "",
      "customerSupplierFlag": "Customer"
    },

我的PHP代码如下:

$array = json_decode($result, true);


for ($i = 0; $i < count($array['$resources']); $i++){
    foreach ($array['$resources'][$i]['customerSupplierFlag'][Customer] as $item)
    {
        // do what you want
        echo $item['customerSupplierFlag' ]. '<br>';
        echo $item['$uuid'] . '<br>';
    }
}

我已经为此苦苦挣扎了几天,看着同样的文章却一无所获。欢迎任何建议。


你的json中没有$array['$resources'][$i]['customerSupplierFlag'][Customer]元素。只有$array['$resources'][$i]['customerSupplierFlag'] - u_mulder
客户是customerSupplierFlag字段中保存的值,我希望过滤它。我相信这类似于xpath中使用的('//$resources[$uuid>=87a132a9-95d3-47fd-8667-77cca9c78436][customerSupplierFlag="Customer"]')表达式。 - Artful_dodger
这里不是xpath,而是一个普通的数组,从JSON中接收到。 - u_mulder
3个回答

13
$data = file_get_contents('./your_json_data.txt');

$json = json_decode($data, 1);

$resources = $json['$resources'];

$suppliers = array();
$customers = array();

foreach ($resources as $rkey => $resource){

    if ($resource['customerSupplierFlag'] == 'Customer'){

        $customers[] = $resource;

    } else if ($resource['customerSupplierFlag'] == 'Supplier') {

        $suppliers[] = $resource;

    }

}

header('Content-Type: application/json');

echo json_encode($customers);
echo json_encode($suppliers);

die();

根据JSON数据的来源和更新频率,您可能考虑将过滤后的数据存储在JSON文件中,然后可以直接通过Web服务器传递JSON,而无需在每个请求上加载PHP并过滤数据。 - JamesWilson

5
$array = json_decode($json, true);
$flag = "Supplier";
$resources = array_filter($array, function ($var) use ($flag) {
    return ($var['customerSupplierFlag'] == $flag);
});
print_r($resources);

1
请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Mark Rotteveel
代码并不复杂,非常直观。只需使用两个基本的内置PHP函数即可。请参考PHP文档:
  1. json_decode:https://www.php.net/manual/zh/function.json-decode.php
  2. array_filter:https://www.php.net/manual/zh/function.array-filter.php
- jai3232
重点不在于代码是否复杂,而是解释能够帮助提问者和未来的访问者理解你的解决方案。答案不应该只是盲目地复制/粘贴一段代码并祈祷它能正常工作。 - Mark Rotteveel
谢谢你添加这段代码,即使你没有时间解释,我仍然非常感激。 - Frank

1
$json_a = json_decode($string, true);
            
 foreach($json_a as $jsonDataKey => $jsonDataValue){
    foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
                print_r($jsonArrayValue['id']);
                print_r($jsonArrayValue['name']);
        }
          }
                        
            

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接