为什么“echo json_encode”不打印数组?

时间:2018-05-15 08:47:34

标签: php sql arrays json backend

首先我有这个:

$myArray = getDatabaseData($p1);

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

echo json_encode($myArray );

这很好用,json数据的回声正常工作。

但是现在我想添加另一个数组数据从其他查询接收数据。像这样:

$myArray = getDatabaseData($p1); //returns 2 dimensions array
$myArray2 = getDatabaseData2($p2); //returns 2 dimensions array

$finalArray = array();
$finalArray['data1'] = $myArray;
$finalArray['data2'] = $myArray2;

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

echo json_encode($finalArray);

并没有回应。

我发现如果我做echo json_encode($myArray);它就会产生回声。 但是,如果我echo json_encode($myArray2);它没有。

注意: getDatabaseData函数只对数据库进行一次查询。 getDatabaseData2执行4,然后将它们组合在一个数组中。 是因为我在我的getDatabaseData2函数中组合了多个数据库查询吗?

以下是$myArray$myArray2 print_r

myArray的:

Array
(
    [values] => Array
        (
            [0] => 0
            [1] => 2
            [2] => 3
            [3] => 2
            [4] => 7
            [5] => 17
            [6] => 6
            [7] => 5
            [8] => 9
            [9] => 0
        )

    [keys] => Array
        (
            [0] => G. M.
            [1] => G. S.
            [2] => Cruz.
            [3] => At.
            [4] => Rem. C.
            [5] => Rem. S.
            [6] => Fs.
            [7] => Rec.
            [8] => B. P.
            [9] => V. F.
        )

)

myArray2:

Array
(
    [names] => Array
        (
            [77] =>     André  
            [78] => Daniel
            [79] => Rúben
            [80] =>     Ant�nio 
            [81] => João 
            [83] =>     João 
        )

    [nums] => Array
        (
            [77] => 0
            [78] => 2
            [79] => 0
            [80] => 0
            [81] => 0
            [83] => 6
        )

    [nums2] => Array
        (
            [77] => 0
            [78] => 0
            [79] => 4
            [80] => 0
            [81] => 3
            [83] => 0
        )    
)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我在这里试过但它的工作!

<!DOCTYPE html>
<html>
    <head>
        <title>My Title</title>
    </head>
    <body>
        <?php
            $arr = array(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5),array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5));
            $arr2 = array(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5),array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5),array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5));

            $finalArray = [];

            $finalArray[] = $arr;
            $finalArray[] = $arr2;

            echo json_encode($finalArray); die();
        ?>
    </body>
</html>