Array_Diff仅从对象数组中删除键/值,而不从整个对象中删除

问题描述 投票:0回答:1

我有一个最终会产生JSON数组的脚本。生成JSON数组的效果很好,但在搜索特定的键值时,我似乎无法从对象数组中删除整个对象。

产生数组:

$sql="SELECT * FROM `emails` WHERE `subject` LIKE '%5678%' order by `id` DESC LIMIT 50";
$result = mysqli_query($DatabasePointer2,$sql) or die(mysqli_error($DatabasePointer2));
$row_cnt = mysqli_num_rows($result); 
if($row_cnt>0) {
    $array = array();
    $i=0;
    while($row = mysqli_fetch_array($result)) {
        $i++;
        $a1 = explode('-', $row['body']);
        $a1[0] = str_replace("","",$a1[0]);
        $a1[1] = str_replace("TF: ","",$a1[1]);
        $a1[1] = str_replace("minutes","M",$a1[1]); 
        $a = array(
            "p" => "test", 
            "id" => $row['id'], 
            "thekey" => $a1[1], 
            "time" => $dt->format('Y-m-d H:i:s'), 
            "utctime" => strtotime($dt->format('Y-m-d H:i:s')), 
            "utc2" =>  $two->format('Y-m-d H:i:s'), 
            "utc3" =>  $three->format('Y-m-d H:i:s')
            );
        $a1 = array_diff($a, ["keytoexclude1", "keytoexclude2", "keytoexclude3"]);

        $array[] = $a1;
    }       
}
echo json_encode($array, JSON_UNESCAPED_SLASHES);

请注意,thekey的值不是静态的。

上面的代码产生了这个:

[{
    "p": "test",
    "id": "198645",
    "time": "2020-05-06 23:00:49",
    "utctime": 1588806049,
    "utc2": "2020-05-07 01:00:49",
    "utc3": "2020-05-07 02:00:49"
}, {
    "p": "test",
    "id": "198605",
    "time": "2020-05-06 22:00:55",
    "utctime": 1588802455,
    "utc2": "2020-05-07 00:00:55",
    "utc3": "2020-05-07 01:00:55",
    "thekey": "123456"
}, {
    "p": "test",
    "id": "198604",
    "time": "2020-05-06 22:00:54",
    "utctime": 1588802454,
    "utc2": "2020-05-07 00:00:54",
    "utc3": "2020-05-07 01:00:54"
}]

[我要完成的工作是搜索与排除列表匹配的任何键,然后如果整个对象在排除列表中找到一个键,则删除整个对象。

排除列表/要删除的代码...

$a1 = array_diff($a, ["keytoexclude1", "keytoexclude2", "keytoexclude3"]);

发生的事情是,它只是删除了键/值本身,而保留了对象的其余部分。如果您在上面的数组中查找,将会看到,如果找到了排除项,则只会从对象中删除“ thekey”及其值,而将其余键保留在对象中。

有人可以协助去除整个物体吗?

**更新以显示错误,其中“ thekey”的值不应该来自mysql数据库,而是爆炸的数组。

php arrays json object
1个回答
热门问题
推荐问题
最新问题