0

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
    It's because of accentued characters. Look this thread https://stackoverflow.com/questions/6928982/how-to-json-encode-array-with-french-accents?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Med May 15 '18 at 08:50
  • I was so confused at `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.` because you gave us a json output of `$myArray2` right at the end of the question lol. If Med's link doesn't solve your question, could go please go into more detail of your expected output and exactly what "dosent work" – IsThisJavascript May 15 '18 at 08:51
  • 1
    Might be worth reading [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279) – CD001 May 15 '18 at 08:56
  • I have posted my answer ! – Manish Goswami May 15 '18 at 09:20
  • @IsThisJavascript That result was from a print_r() not echo json –  May 15 '18 at 09:22
  • @Med It appears: "Detected an illegal character in input string" but it doesn't remove the accents. –  May 15 '18 at 09:28

1 Answers1

1

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>
Manish Goswami
  • 442
  • 4
  • 10