php 多维数组的 JSON_ENCODE 给出不同的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10778741/
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-24 23:02:38  来源:igfitidea点击:

JSON_ENCODE of multidimensional array giving different results

phparraysjson

提问by dangel

When doing a json_encode a multidimensional array in PHP, I'm noticing a different output simply by naming one of the arrays, as opposed to not naming them. For Example:

在 PHP 中对多维数组执行 json_encode 时,我注意到仅通过命名其中一个数组而不是不命名它们就会产生不同的输出。例如:

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

gives a single array of multiple json objects

给出多个 json 对象的单个数组

[{"a":1,"b":2},{"c":3},{"d":4}];


whereas simply assigning a name to the middle array

而简单地为中间数组分配一个名称

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

creates a single json object with multiple json objects inside

创建一个包含多个 json 对象的单个 json 对象

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

why would the 1st option not return the same reasults as the 2nd execpt with "1" in place of "secondarray"

为什么第一个选项不会返回与第二个执行相同的结果,用“1”代替“secondarray”

回答by Michael Berkowski

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.

在 JSON 中,数组[]只有每个都有数字键,而对象{}具有字符串属性。在您的第二个示例中包含数组键强制整个外部结构必须成为一个对象。由于包含字符串 keys ,两个示例的内部对象都被制作为对象a,b,c,d

If you were to use the JSON_FORCE_OBJECToption 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.

如果您要JSON_FORCE_OBJECT在第一个示例中使用该选项,您应该返回与第二个类似的结构,外部结构是一个对象而不是数组。没有指定您希望它作为对象,外部数组中缺少字符串键会导致 PHP 假定它被编码为 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}}

回答by Felix Kling

Arrays with continuous numerical keys are encoded as JSON arrays. That's just how it is. Why? Because it makes sense.

具有连续数字键的数组被编码为 JSON 数组。就是这样。为什么?因为有道理。

Since the keys can be expressed implicitly through the array encoding, there is no reason to explicitly encoded them as object keys.

由于键可以通过数组编码隐式表达,因此没有理由将它们显式编码为对象键。

See all the examples in the json_encodedocumentation.

请参阅json_encode文档中的所有示例。

回答by Fernando

At the first option you only have numeric indexes (0, 1 and 2). Although they are not explicitly declared, php automatically creates them.

在第一个选项中,您只有数字索引(0、1 和 2)。尽管它们没有明确声明,但 php 会自动创建它们。

At the second option, you declare one of the indexes as an string and this makes PHP internally transform all indexes to string.

在第二个选项中,您将其中一个索引声明为字符串,这使得 PHP 在内部将所有索引转换为字符串。

When you json encode the first array, it's not necessary to show the integers in the generated json string because any decoder should be able to "guess" that they are 0, 1 and 2.

当您对第一个数组进行 json 编码时,没有必要在生成的 json 字符串中显示整数,因为任何解码器都应该能够“猜测”它们是 0、1 和 2。

But in the second array, this is necessary, as the decoder must know the key value in your array.

但在第二个数组中,这是必要的,因为解码器必须知道数组中的键值。

It's pretty simple. No indexes declared in array? Them they are 0, 1, 2, 3 and so on.

这很简单。数组中没有声明索引?它们是 0、1、2、3 等等。

回答by Nabeel Ali

output of this as in json form is year1{a,b},year2{c}, year3{d} **a has value 1 ,b=2,c=3,d=4 stored in array of year1's a,b years2's c and years3's d respectivily

json 形式的输出是 year1{a,b},year2{c}, year3{d} **a 的值 1 ,b=2,c=3,d=4 存储在 year1 的 a,b 数组中年 2 的 c 和年 3 的 d 分别

$array1 = array('a'=>1, 'b'=>2);
    $array2 = array('c'=>3);
    $array3 = array('d'=>4)
    $form = array("year1" =>$array1,
                  "year2" =>$array2,
                  "year3" =>$array3,
            );

    $data = json_encode($form);