gpt4 book ai didi

php - 使用 php 过滤 JSON 数据

转载 作者:可可西里 更新时间:2023-11-01 00:05:08 25 4
gpt4 key购买 nike

我正在尝试遍历这个 json 文件并过滤掉不需要的元素我想拆分结果所以我有一个客户列表或一个供应商列表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>';
}
}

几天来我一直在努力解决这个问题,现在似乎正在阅读相同的文章,如果有任何建议,我们将不胜感激。

最佳答案

$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();

关于php - 使用 php 过滤 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com