gpt4 book ai didi

javascript - 如何使用php或jquery过滤json?

转载 作者:行者123 更新时间:2023-12-03 09:33:11 25 4
gpt4 key购买 nike

    [
{
"Location":"DLI",
"20FR":"0",
"20GP":"6",
"20HC":"0",
"20HD":"0",
"20OT":"0",
..,,
..,,
"Total":"7",
"Cost":"251500.000"
},
{
"Location":"LKCMB",
"20FR":"0",
"20GP":"0",
"20HC":"0",
"20HD":"0",
"20OT":"0",
..,,
..,,
"Total":"9",
"Cost":"360000.000"
},
{
"Location":"MAA",
"20FR":"0",
"20GP":"12",
"20HC":"0",
"20HD":"0",
"20OT":"1",
..,,
..,,
"Total":"20",
"Cost":"1041183.000"
}
etc.,
]

我有上面给出的 json 输出,现在我需要根据键值过滤此 json。例如,在上面的json中,20FR在每个数组中都是0,所以我需要过滤它,20GP在所有数组中没有0,所以不需要过滤这个,同样,20HC和20HD都是0,必须再次过滤20OT是一个数组中不是 0,因此不需要像我必须过滤 json 那样进行过滤。请有人帮助我。这就是期望的输出。

    [
{
"Location":"DLI",
"20GP":"6",
"20OT":"0",
..,,
..,,
"Total":"7",
"Cost":"251500.000"
},
{
"Location":"LKCMB",
"20GP":"0",
"20OT":"0",
..,,
..,,
"Total":"9",
"Cost":"360000.000"
},
{
"Location":"MAA",
"20GP":"12",
"20OT":"1",
..,,
..,,
"Total":"20",
"Cost":"1041183.000"
}
etc.,
]

请有人帮助我如何在 php、jquery 或 javascript 中执行此操作。谢谢

最佳答案

你可以做这样的事情。我不确定这是最有效的方法,但它可以完成工作。我做了一个检查你的 json 的函数。

<?php

$oldJson = '[
{
"Location":"DLI",
"20FR":"0",
"20GP":"6",
"20HC":"0",
"20HD":"0",
"20OT":"0",
"Total":"7",
"Cost":"251500.000"
},
{
"Location":"LKCMB",
"20FR":"0",
"20GP":"0",
"20HC":"0",
"20HD":"0",
"20OT":"0",
"Total":"9",
"Cost":"360000.000"
},
{
"Location":"MAA",
"20FR":"0",
"20GP":"12",
"20HC":"0",
"20HD":"0",
"20OT":"1",
"Total":"20",
"Cost":"1041183.000"
}
]';

function filterJson($json){

//Decodes a JSON string, When TRUE, it will be converted into associative arrays.
$array = json_decode($json, true);

//count number of inner arrays
$nbr = count($array);

//get all keys of the inner array
$keys = array_keys($array[0]);

//iterate through the keys
foreach($keys as $key){

//check every inner array per key
for($x=0; $x<$nbr; $x++){

//if not 0 than break
if($array[$x][$key] != "0"){
break;
}

//if we didn't break before the last array, all values are 0 and we can unset those values.
if($x == $nbr-1){

//iterate through the arrays
for($x=0; $x<$nbr; $x++){
unset($array[$x][$key]);
}

}
}
}

$json = json_encode($array);
return $json;
}

$newJson = filterJson($oldJson);

echo $newJson;

?>

关于javascript - 如何使用php或jquery过滤json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430164/

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