我希望 PHP 中的 json_encode 返回一个 JSON 数组,即使索引不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3559703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:13:46  来源:igfitidea点击:

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

phpjsonjsonpflot

提问by Andrew Watson

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

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

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:

所以漂浮是窒息。如果我在调用 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)
}

any ideas?

有任何想法吗?

回答by pr1001

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_valuesbefore json_encode:

正如 zneak 所说,Javascript(以及 JSON)数组不能有乱序的数组键。因此,您要么需要接受您将使用 JSON 对象而不是数组,要么调用array_valuesbefore 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:

给定您的数组(我们称之为$data),我们可以像这样获得正确的 JSON:

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

回答by zneak

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

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

As a reminder, a JSON array looks like this:

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

[1, 2, 3, 4, 5]

There's no room to put indices there.

没有空间放置索引。

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

您应该在 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;
}

回答by Marc B

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()通过将 TRUE 作为第二个参数传递来强制生成数组,但您不能首先强制json_encode()生成数组:

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

回答by Robin

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

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

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

回答by pkh

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:

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

  [[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.

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