user9324524
user9324524

Reputation:

Why "echo json_encode" is not printing the array?

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.

Upvotes: 0

Views: 791

Answers (1)

Manish Goswami
Manish Goswami

Reputation: 422

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>

Upvotes: 0

Related Questions