PHP>;如何在使用json_encode时跳过元素


PHP > How to skip an element while using json_encode

我需要以JSON格式向客户端发送数据。

到目前为止,它在json_encode上运行得很好,如下所示:

$sql = "SELECT `card`.`CardID`,`card`.`Text`,... WHERE (`card`.`CardID`= :ItemId)";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':ItemId', $itemid);   
$stmt->execute();
while ($row = $stmt->fetchObject()) {
    $posts[]=$row;
}
...
...
$res["rows"] = $posts;
$res["maptel"] = $maptelindicator;
echo json_encode($res,JSON_UNESCAPED_UNICODE); 

但现在我遇到了一个问题。我在DB中有一个新字段(Videofiles),它已经是JSON格式的,存储为SDK函数的输出。如果我再次对这个JSON编码的字段进行编码,我会得到客户端无法读取的数据。。

我如何对所有其他数据进行json_encode,但跳过这个特定的字段,视频文件?

以下是使用print_r:的示例数据结构的输出

Array
(
    [rows] => Array
        (
            [0] => stdClass Object
                (
                    [Name] => Company13
                    [CID] => 26
                    [CardID] => 26-000002
                    [Text] => Kleopatra Deluxe Hotel reservations
                    [ImageLink] => 
                    [VideoFiles] => [{"quality":"mobile","type":"video/mp4","width":480,"height":270....}]
                    [ClickDate] => 2015-11-03
                )
        )
    [maptel] => 0
)

谢谢。。。

您可以使用json_decode对已经编码的字段进行解码,然后将结果添加到$res数组中。

最好的方法是在SQL中删除此部分!

但是,如果你想在php中删除,这将给你带来成本,你可以在这种情况下使用unset;

foreach ($posts as $post){
    unset($posts['VideoFiles']);
}
$res["rows"] = $posts;
$res["maptel"] = $maptelindicator;
echo json_encode($res,JSON_UNESCAPED_UNICODE);