douzhan1031 2018-05-15 08:47
浏览 96

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

First i had this:

$myArray = getDatabaseData($p1);

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

echo json_encode($myArray );

This was working well, the echo of json data was working correctly.

But now i wanted to add another array data receives data from other query. Like this:

$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);

And is not doing the echo.

I've discovered that if i do echo json_encode($myArray); it does the echo. But if i do echo json_encode($myArray2); it does not.

Note: getDatabaseData function does only one query to the DB. getDatabaseData2 does 4, that are then combined in a single array. Is it because i am combining multiple db queries in my getDatabaseData2 function?

Here is the print_r of $myArray and $myArray2 :

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
        )    
)

Thanks for the help.

  • 写回答

1条回答 默认 最新

  • duanpendan8067 2018-05-15 09:20
    关注

    Here I tried but its working!

    <!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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程