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 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块