我在PHP Like中有一个JSON对象
$my_json = '[{
"No": "1",
"Id": "10",
"msg": "Value is 10"
},
{
"No": "2",
"Id": "55",
"msg": "value is 55"
}, {
"No": "3",
"Id": "38",
"msg": "value is 38 "
}, {
"No": "4",
"Id": "95",
"msg": "value is 95 "
} ]';
我已使用$ obj_arr = json_decode($ my_json);将其转换为PHP对象数组;
现在,基于用户选择,我想过滤数组元素 例如。如果用户选择“ ID”为38和55,则新数组应注意其ID为10和95,并且必须包含与该ID相关的所有其他数据,就像原始数据一样
EG
$final_json = '[
{
"No": "2",
"Id": "55",
"msg": "value is 55"
}, {
"No": "3",
"Id": "38",
"msg": "value is 38 "
} ]';
答案 0 :(得分:0)
尝试一下:
const inputScript = `
function test(callback, number, reason) {
if (number != atob("MA==")) {
console.log(atob("RmFpbGVkOiA=") + reason + atob("LiBQbGVhc2UgdHJ5IGFnYWluLg=="));
} else {
callback(number);
}
}
`;
const outputScript = inputScript
.replace(/atob\("([^\"]+)"\)/g, (_, p1) => `"${atob(p1)}"`)
.trim();
console.log(outputScript);
输出:
<?php
$my_json = '[{
"No": "1",
"Id": "10",
"msg": "Value is 10"
},
{
"No": "2",
"Id": "55",
"msg": "value is 55"
}, {
"No": "3",
"Id": "38",
"msg": "value is 38 "
}, {
"No": "4",
"Id": "95",
"msg": "value is 95 "
} ]';
$selected = [38, 55];
$obj = json_decode($my_json);
$result = array_filter($obj, function($row) use ($selected) {
return in_array($row->Id, $selected);
});
echo json_encode(array_values($result), JSON_PRETTY_PRINT);
如果要过滤掉上面选择的值而不是保留它们,请返回in_array的取反。