为什么json_encode对这个特定的数组进行编码,而对具有相同结构的其他数组进行编码?


Why won't json_encode encode this particular array but encodes other arrays with same structure?

我制作了一个API,将数据从表编码为数组,然后再编码为json。

 <?php
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
...(connection to db)... :p
        $query = "SELECT * FROM gdb.".$_GET['tb'];
        $result = mysqli_query($conn,$query);

$posts =array();
while ($row = mysqli_fetch_assoc($result))
{
    $posts[] = array('ONE'=>$row);


}
$all = array('ALL'=>$posts);
echo json_encode($all);//($posts);
mysqli_close($conn);
?>

它似乎与其他表工作良好,但与这个特定的表,json_encode似乎不工作。

这是表中不会被编码为json的数组:

array(1) {
  ["ALL"]=>
  array(61) {
    [0]=>
    array(1) {
      ["ONE"]=>
      array(12) {
        ["id_product"]=>
        string(1) "2"
        ["id_shop"]=>
        string(1) "1"
        ["id_lang"]=>
        string(1) "1"
        ["description"]=>
        string(72) "<p> BUY 5-KILOS  RICE FREE SARDINES 155G.SAVE 10.00</p>"
        ["description_short"]=>
        string(0) ""
        ["link_rewrite"]=>
        string(20) "5-kilos-rice"
        ["meta_description"]=>
        string(0) ""
        ["meta_keywords"]=>
        string(0) ""
        ["meta_title"]=>
        string(0) ""
        ["name"]=>
        string(40) "5-KILOS RICE FREE SARDINES"
        ["available_now"]=>
        string(0) ""
        ["available_later"]=>
        string(0) ""
      }
    }
    [1]=>
    array(1) {
      ["ONE"]=>
      array(12) {
        ["id_product"]=>
        string(1) "3"
        ["id_shop"]=>
        string(1) "1"
        ["id_lang"]=>
        string(1) "1"
        ["description"]=>
        string(78) "<p>BUY 10-KILOS RICE FREE SARDINES RED 155G.SAVE 20.00</p>"
        ["description_short"]=>
        string(0) ""
        ["link_rewrite"]=>
        string(21) "10-kilos-rice"
        ["meta_description"]=>
        string(0) ""
        ["meta_keywords"]=>
        string(0) ""
        ["meta_title"]=>
        string(0) ""
        ["name"]=>
        string(41) "10-KILOS RICE FREE SARDINES"
        ["available_now"]=>
        string(0) ""
        ["available_later"]=>
        string(0) ""
      }
    }
}}

数组从那些我的代码能够编码为json具有相同的结构。只是字段和内容的数量不同,所以我不知道为什么这个特定的数组不能工作。

我想。也许一些描述字符串(不包括所有在我的样本)有引号,双引号,斜杠,这就是为什么它不能编码为json,所以我在while循环中这样做:

$posts[] =  array('ONE'=>array_map("addslashes",$row));

而不是:

$posts[] = array('ONE'=>$row);

,但它仍然没有编码为json。我使用array_map错误吗??还是有其他原因不能将其编码为json?

我只需要添加这个

mysqli_set_charset($conn,"utf8");

before mysqli_select_db() after all:B