简体   繁体   English

如何推入json数组?

[英]How to push into json array?

var array = ["History"]
...
for (var i = 0; i<div.length; i++){
    var array2 = {
        "name": div[i].value
    };
}
array.push(array2);

I need to create root of array as showing below 我需要创建数组的根,如下所示

"History":{
    0:{
        name: "some text"
    },
    1:{
        name: "some text 2"
    }
}

But in result there is no parent HISTORY as showing below. 但是结果是,没有父历史记录,如下所示。 How can I create child History and into the child push as showing above? 如上图所示,我如何创建子历史记录并将其添加到子项中?

0: "History",
1: {
    name: "some text"
},
2: {
    name: "some text 2"
}

Easy ES6 example. 简单的ES6示例。

const tag = 'History';
const obj = {[tag]: {}};
const res = ['ex1', 'ex2', 'ex3'].reduce((acc, x, i) => {acc[tag][i] = x; return acc;}, obj);

console.log(res); // { "History": { 0: "ex1", 1: "ex2", 2: "ex3" } }

Old browser support. 旧的浏览器支持。

var tag = 'History';
var obj = {History: {}};
var res = ['ex1', 'ex2', 'ex3'].reduce(function(acc, x, i){ acc[tag][i] = x; return acc; }, obj);

console.log(res); // Same output as above

Make the History an element of array object (and give it proper name...). 将History设为数组对象的元素(并为其指定适当的名称...)。 then History can be a root element of array' in it you can use push function: 然后历史记录可以是数组的根元素”,您可以使用push函数:

var array = {"History":[]};
...
array.History.push(array2);

Declare the value of array["History"] as an array and push values into that one: 将array [“ History”]的值声明为一个数组,并将值压入该数组:

    var array = ["History"]
    array["History"] = [];
    ...
    for (var i = 0; i<div.length; i++){
        array["History"].push( {
            "name": div[i].value
        });
    }

There is no thing like a "json array" or "json object" . 没有像“ json数组”“ json对象”之类的东西 JSON is always text. JSON始终是文本。 It is a text representation of a data structure that can be an array or an object. 它是数据结构的文本表示形式,可以是数组或对象。 It uses the JavaScript notation for arrays and objects. 它对数组和对象使用JavaScript表示法。

In order to get this JSON: 为了获得此JSON:

"History":{
    0:{
        name: "some text"
    },
    1:{
        name: "some text 2"
    }
}

you need to build a PHP object or array . 您需要构建一个PHP 对象数组 PHP arrays are more versatile than bare objects and I recommend you use them. PHP 数组裸对象更通用,我建议您使用它们。

PHP arrays whose keys are numeric, consecutive and starting from zero are encoded as arrays in JSON. 键为数字,连续且从零开始的PHP数组被编码为JSON中的数组。 The other arrays are encoded as objects. 其他数组被编码为对象。

This array must have only one key ( "History" ) and its associated value must be an array (numerically indexed) that contains two arrays. 此数组必须只有一个键( "History" ),并且其关联的值必须是包含两个数组的数组(数字索引)。 Each of these arrays must have only one key: "name" . 这些数组中的每一个必须只有一个键: "name"

Your array would be like this: 您的数组如下所示:

$input = array(
    "History" => array(
        array("name" => "some text"),
        array("name" => "some text 2"),
    ),
);

When passed to json_encode() it produces a JSON equivalent to the one you expect. 当传递给json_encode()它会生成与您期望的JSON等效的JSON。

There are countless ways to build the input array. 建立输入数组的方法有无数种。 Read more about accessing and modifying array elements using the square brackets syntax . 阅读更多有关使用方括号语法访问和修改数组元素的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM