我希望 PHP 中的 json_encode 返回 JSON 数组,即使索引不按顺序排列

发布于 2024-09-15 19:28:49 字数 784 浏览 5 评论 0原文

但根据这个: http://www.php.net/ Manual/en/function.json-encode.php#94157 它不会。

我正在使用 flot,所以我需要一个返回数字索引的数组,但我得到的是:

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

所以 flot 令人窒息。如果我在调用 json_encode 之前 var_dump 数组,它看起来像这样:

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃兔兔 2024-09-22 19:28:49

正如 zneak 所说,Javascript(以及 JSON)数组不能有乱序的数组键。因此,您要么需要接受您将使用 JSON 对象,而不是数组,要么在 json_encode 之前调用 array_values

json_encode(array_values($data));

但是,看起来您想要用flot显示时间序列数据。正如您在 flot 时间序列示例 中看到的,它应该是像这样的两个元素数组:

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

给定你的数组(我们称之为 $data),我们可以获得正确的 JSON,如下所示:

json_encode(
  array_map(
    function($key, $value) { return array($key, $value); },
    array_keys($data),
    array_values($data)
  )
);

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)
  )
);
标点 2024-09-22 19:28:49

这在概念上是不可能的。您无法在 JSON 中对具有固定索引的数组进行编码。

提醒一下,JSON 数组如下所示:

[1, 2, 3, 4, 5]

那里没有空间放置索引。

你应该在 Javascript 方面工作。接受 json_encode 将返回一个对象,您可以将该对象转换为数组。那应该不会太难。

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;
}

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;
}
给不了的爱 2024-09-22 19:28:49

您可以通过传递 TRUE 作为第二个参数来强制 json_decode() 生成数组,但不能强制 json_encode() 首先生成数组:

json_decode($json, TRUE); // force array creation

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
缪败 2024-09-22 19:28:49

您可以使用 array_merge 重新索引数字索引数组,如下所示:

$a = array(2 => 3, 4 => 5);
$a = array_merge($a);
var_dump($a);

You can use array_merge to reindex a numerically indexed array, like this:

$a = array(2 => 3, 4 => 5);
$a = array_merge($a);
var_dump($a);
携余温的黄昏 2024-09-22 19:28:49

对于flot来说,你所要求的并不是你真正想要的。您需要一个数组的数组,而不是一个数字的数组。也就是说,您想要看起来像这样的东西:

  [[1281830400, 34910],
   [1281916800, 45385],
   [1282003200, 56928],
   [1282089600, 53884],
   [1282176000, 50262],
   [1281657600, 45446],
   [1281744000, 34998]]

至于如何在 PHP 中做到这一点,我不确定。

For flot, what you're asking for isn't actually what you want. You want an array of arrays, not an array of numbers. That is, you want something that looks like this:

  [[1281830400, 34910],
   [1281916800, 45385],
   [1282003200, 56928],
   [1282089600, 53884],
   [1282176000, 50262],
   [1281657600, 45446],
   [1281744000, 34998]]

As for how to do that in PHP, I'm not sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文