Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like json_encode in PHP to return a JSON array even if the indices are not in order

Tags:

json

php

jsonp

flot

but according to this: http://www.php.net/manual/en/function.json-encode.php#94157 it won't.

I'm using flot so I need to have an array with numeric indexes returned but what I'm getting is this:

jsonp1282668482872 ( {"label":"Hits 2010-08-20","data":{"1281830400":34910,"1281916800":45385,"1282003200":56928,"1282089600":53884,"1282176000":50262,"1281657600":45446,"1281744000":34998}} );

so flot is choking. If I var_dump the array right before I call json_encode it looks like this:

array(7) {
  [1281830400]=>
  int(34910)
  [1281916800]=>
  int(45385)
  [1282003200]=>
  int(56928)
  [1282089600]=>
  int(53884)
  [1282176000]=>
  int(50262)
  [1281657600]=>
  int(45446)
  [1281744000]=>
  int(34998)
}

any ideas?

like image 574
Andrew Watson Avatar asked Aug 24 '10 18:08

Andrew Watson


People also ask

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

What does json_encode return?

The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What is the difference between json_encode and json_decode?

I think json_encode makes sure that php can read the . json file but you have to specify a variable name, whereas with json_decode it's the same but you have to specify a file name.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.


3 Answers

It's conceptually impossible. You cannot encode an array with fixed indices in JSON.

As a reminder, a JSON array looks like this:

[1, 2, 3, 4, 5]

There's no room to put indices there.

You should work on the Javascript side. Accepting that json_encode will return an object, you can convert this object into an array. That shouldn't be too hard.

function toArray(object)
{
    var result = [];
    for (var key in object)
    {
        if (!key.match(/^[0-9]+$/)) throw new Error("Key must be all numeric");
        result[parseInt(key)] = object[key];
    }
    return result;
}
like image 158
zneak Avatar answered Sep 25 '22 09:09

zneak


As zneak says, Javascript (and thus JSON) arrays cannot have out-of-order array keys. Thus, you either need to accept that you'll be working with JSON objects, not arrays, or call array_values before json_encode:

json_encode(array_values($data));

However, it looks like you're looking to display time series data with flot. As you can see on the flot time series example, it should be a two element array like so:

$.plot(
  $('#placeholder'),
  [[
    [1281830400, 34910],
    [1281916800, 45385],
    [1282003200, 56928],
    [1282089600, 53884],
    [1282176000, 50262],
    [1281657600, 45446],
    [1281744000, 34998]
  ]],
  {
    label: 'Hits 2010-08-20',
    xaxis: {mode: 'time'}
  }
)

Given your array (let's call it $data) we can get the proper JSON like so:

json_encode(
  array_map(
    function($key, $value) { return array($key, $value); },
    array_keys($data),
    array_values($data)
  )
);
like image 24
pr1001 Avatar answered Sep 24 '22 09:09

pr1001


You can force json_decode() to produce arrays by passing TRUE as the second parameter, but you can't force json_encode() to produce arrays in the first place:

json_decode($json, TRUE); // force array creation
like image 38
Marc B Avatar answered Sep 21 '22 09:09

Marc B