将php数组转换为json

时间:2015-04-24 08:25:29

标签: php arrays json multidimensional-array

我有以下代码

$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
$posts = array();
if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) 
            {
               array_push($posts , $row);
            }
    }
echo"<pre>";
  print_r($posts);
echo "</pre>";

header('Content-type: application/json');
echo json_encode('list'=>$posts);

$ posts包含我现在需要的数组。我希望将其转换为json。我尝试了这两种方法

echo json_encode($posts, JSON_UNESCAPED_UNICODE);
echo json_encode($posts)

但没有任何结果。我的php版本是5.5.21(CGI)

任何人都可以帮助我。

3 个答案:

答案 0 :(得分:1)

在这里试试这段代码......

<?php
header('Content-Type: text/html; charset=utf-8' );

$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn)
    {
        die("Connection failed: " . mysqli_connect_error());
    }
mysqli_query($conn,"set character_set 'utf8'");
mysqli_set_charset($conn, "utf8");
mysqli_query($conn,"SET NAMES 'utf8'");

$sql = "SELECT * FROM vendor ";
$result = mysqli_query($conn, $sql);
$posts = array();
if (mysqli_num_rows($result) > 0) 
    {
       while($row = mysqli_fetch_assoc($result)) 
            {
              array_push($posts , $row);
            }

        echo json_encode(array('list'=>$posts), JSON_UNESCAPED_UNICODE);        
    }
else 
    {
        echo stripslashes(json_encode(array('list'=>'No list available')));
    }    
?>

答案 1 :(得分:0)

为了确保进入循环,你应该在

中添加回声
if (mysqli_num_rows($result) > 0) 
    {
        echo 'Test 1st Loop';
        while($row = mysqli_fetch_assoc($result)) 
            {
               echo 'array_push';
               array_push($posts , $row);
            }
    }

不要忘记添加

header('Content-Type: application/json');

答案 2 :(得分:0)

经过多次尝试,我得到了解决方案。 我已经完成了我的本地数据库表,它对我来说很好。只需看一次。

我刚刚更改了我的表名和数据库名.strest代码相同。

<强>代码: -

    <?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "dboard";
    $table = "appusers";
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    else{
    echo "Connected successfully";
    }

    $sql = "SELECT * FROM $table";
    $result = mysqli_query($conn, $sql);
    $posts = array();
    if (mysqli_num_rows($result) > 0) 
        {
            while($row = mysqli_fetch_assoc($result)) 
                {
                   array_push($posts , $row);
                }
        }
echo"<pre>*********Array Data:******</br>";
 print_r($posts);
echo "</pre>";

echo"<pre>@@@@@@@@@@@@@@@@@@@@Json Format Data:@@@@@@@@@@@@@@@@</br></pre>";

echo json_encode($posts);  
    ?>  

希望这有帮助,

<强>输出: -

Connected successfully

*********Array Data:******
Array
(
    [0] => Array
        (
            [id] => 1
            [lastname] => user
            [firstname] => k
            [user] => raj
        )

    [1] => Array
        (
            [id] => 4
            [lastname] => user
            [firstname] => k
            [user] => yusuf
        )

    [2] => Array
        (
            [id] => 5
            [lastname] => users
            [firstname] => kl
            [user] => raj
        )

)

@@@@@@@@@@@@@@@@@@@@Json Format Data:@@@@@@@@@@@@@@@@

[{"id":"1","lastname":"user","firstname":"k","user":"raj"},{"id":"4","lastname":"user","firstname":"k","user":"yusuf"},{"id":"5","lastname":"users","firstname":"kl","user":"raj"}]