dougou8552 2015-09-22 04:58
浏览 192
已采纳

var_dump显示正确的数组但json_encode函数在尝试编码同一个数组时返回false

I want to encode data retrieved from database into json. Here is my codes..

<?php 
$con = mysqli_connect("localhost", "root", "", "joomla");     

if($con === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}     

$sql = "SELECT * FROM yv084_content";
$result = mysqli_query($con, $sql);

$all_result = array();

$intro_result = array();

$num_rows = mysqli_num_rows($result);

$count =0;
while($row = mysqli_fetch_assoc($result)){          
    $all_result[] = $row;       
}    

while($count<$num_rows){        
    $intro_result[] = strip_tags($all_result[$count]['introtext']);
    //echo strip_tags($all_result[$count]['introtext'])."<hr />";
    $count++;
}

echo "<hr />";
var_dump($intro_result);
echo "<hr />";
$encoded_value = json_encode($intro_result);

var_dump($encoded_value);
echo $encoded_value;

when I use var_dump function, then i got proper result but when i use json_encode function to encode that array it is returning false.. Is there any idea how to encode them..

Note: There is alot of special characters and html entities so I am using strip_tags function.

please review the result what I got in the browser...

Screenshot of browser's result

  • 写回答

2条回答 默认 最新

  • dtx6087 2015-09-22 05:28
    关注

    As per above answer, you cannot convert special characters in array with json_encode. However you can try following:

    $d1 = array("asdadlasdladlasdad adadasdsa", "Nuwakot, Sep. 18: dadasda", "Åland Islands", "<b>test</b>");
    array_walk($d1, function(&$value) {
        $value = utf8_encode($value);
    });
    print_r(json_encode($d1));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line