JSON_Encode of Multidimensional Array Giving Different Results

JSON_ENCODE of multidimensional array giving different results

In JSON, arrays [] only every have numeric keys, whereas objects {} have string properties. The inclusion of a array key in your second example forces the entire outer structure to be an object by necessity. The inner objects of both examples are made as objects because of the inclusion of string keys a,b,c,d.

If you were to use the JSON_FORCE_OBJECT option on the first example, you should get back a similar structure to the second, with the outer structure an object rather than an array. Without specifying that you want it as an object, the absence of string keys in the outer array causes PHP to assume it is to be encoded as the equivalent array structure in JSON.

$arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));

// Force the outer structure into an object rather than array
echo json_encode($arrytest , JSON_FORCE_OBJECT);

// {"0":{"a":1,"b":2},"1":{"c":3},"2":{"d":4}}

json_encode() on a multidimensional array - with string keys

I have now got a working solution which is fast and works well.

  1. Firstly, as written in SO link from the question;

    In JSON, arrays only have numeric keys, whereas objects have string
    properties. The inclusion of a array key forces the entire outer
    structure to be an object by necessity.

    In JSON; Curly braces hold objects ({}), Square brackets hold arrays ([]).

  2. So using a string as a key will result in the json_encode function returning objects, whereas reseting the keys will ensure it creates arrays.

  3. Therefore, just before I return my JSON encoded string, I run a function to reset all the array keys. The code I found on this SO thread (Reset array keys in multidimensional array) was particularly useful!

PHP: json_encode() doesn't show anything with multidimensional array

User @Joni led me to the solution.

Adding mysql_set_charset("utf8") fixed my issue.

As mentioned in this post: Why is this PHP call to json_encode silently failing - inability to handle single quotes?.

json_encode returns Array full of null's

As @Jeff comment: mysqli_query returns a result object.

See mysqli_fetch_all Documentation

Try run:

$data = mysqli_fetch_all($searchresult);
echo json_encode($data );

json_encode with a multidimensional array

Before encoding the JSON, you have already successfully sorted your array $year with array_multisort(). The next thing to do is to iterate it 3 times, one each for your three output variables and produce the 2D array for each one. Then after you have those, you can use PHP to write out the JavaScript and call json_encode() on the individual arrays.

// You already sorted $year how you need it
array_multisort($year, SORT_ASC, $csv);

// This will store your final output
$mapped_values = array();

// You want the keys 1,4,5
foreach (array(1,4,5) as $key) {
$temp = array();
// Loop over each year array and make a sub-array of year,key
// 2008, 2008, 2010....
foreach ($csv as $y) {
// intval() & floatval() prevent the output as strings to JSON...
$temp[] = array(intval($y[0]), floatval($y[$key]);
}
// Append the structure to the outer result
// Each of these corresponds to one of the keys 1,4,5...
$mapped_values[] = $temp;
}

// Write it as output:
foreach ($mapped_values as $varnum => $js_var) {
// To get the d1, d2, d3
$num = $varnum + 1;
// Write a var dN = JSON...
// json_encode() will produce the appropriate output
echo "var d{$num} = " . json_encode($js_var) . ";\n";
}

This produces the following output:

var d1 = [[2008,41],[2009,33],[2010,33],[2012,37],[2013,55]];
var d2 = [[2008,125.2],[2009,99.9],[2010,91.9],[2012,105.8],[2013,115.1]];
var d3 = [[2008,78.48],[2009,68.83],[2010,66.27],[2012,68.61],[2013,68.29]];

JSON loop gives one array more than expected

You should place the echo statement outside the foreach loop:

$files = glob("players/*");
$nFiles = count($files);

foreach($files as $file){

$jsonArray[] = array(

"name" => "a name",
"reason" => "a reason",
"date" => "a date"

);
}
echo json_encode($jsonArray);

json_encode with multidimensional array returning string array instead of data

This is because you are casting $proj_option to a string by enclosing it in quotes. Just remove the quotes and you will get the array.



Related Topics



Leave a reply



Submit