简体   繁体   English

PHP对象数组到javascript关联数组

[英]PHP Object array to javascript associative array

I have a PHP object array as follows: 我有一个PHP对象数组,如下所示:

array(223) {
  [0]=>
  object(stdClass)#5 (9) {
    ["id"]=>
    string(2) "10"
    ["name"]=>
    string(10) "Cyclops"
    ["address"]=>
    string(13) "GSDG"
  }
  [1]=>
  object(stdClass)#6 (9) {
    ["id"]=>
    string(2) "11"
    ["name"]=>
    string(11) "Professor X"
    ["address"]=>
    string(8) "XX"
  }

I need to convert it into a javascript array with the name and id in this format: 我需要将其转换为具有以下格式的名称和ID的javascript数组:

var characters = {
    data: [{
            "name": "Cyclops",
            id: 10
        },
        {
            "name": "Professor X",
            id: 11
        }
    ]
}

Here is what I have tried: 这是我尝试过的:

First I converted the PHP arrays to javascript objects and then used a for loop to push details of each array to a javascript object: 首先,我将PHP数组转换为javascript对象,然后使用for循环将每个数组的详细信息推送至javascript对象:

var js_data = <?php echo json_encode($chars) ?>;
var characters = [];

for (var i = 0; i < js_data.length; i++) {
    characters.push({
        data: js_data[i]
    })
}

But this results in an array of objects, each having a "data" property. 但这会导致对象阵列,每个对象都具有“数据”属性。 Here is the console.log of testVariable: 这是testVariable的console.log:

0: {…}
​​​
data: Object { id: "10", name: "Cyclops", … }
​​​
__proto__: Object { … }
​​
1: {…}
​​​
data: Object { id: "11", name: "Professor X", … }

How do I fix this? 我该如何解决?

Isn't the hardest task, as you almost have your desired format. 这不是最困难的任务,因为您几乎可以拥有所需的格式。 At first I have to add, that you possibly even not have to filter out the address field, as the plugin may ignore it completly. 首先,我必须补充一点,您可能甚至不必过滤出地址字段,因为插件可能会完全忽略它。 If this is the case, you could simply do this 如果是这种情况,您可以简单地这样做

var characters = <?php json_encode(['data' => $chars]); ?>;

And if you really need to filter out the address field, you could use array_map . 如果确实需要过滤出地址字段,则可以使用array_map

$chars = array_map(function($item) {
    // at this point, you don't need an object, as json_encode do the
    // same for objects and associative arrays
    return [
        "id" =>$item->id,
        "name" => $item->name
    ];
}, $chars);

You can convert the last javascript JSON array to 您可以将最后一个javascript JSON数组转换为

var characters = {
data: [{
        "name": "Cyclops",
        id: 10
    },
    {
        "name": "Professor X",
        id: 11
    }
]

} }

By using your PHP code result 通过使用您的PHP代码结果

0: {…}

​​ data: Object { id: "10", name: "Cyclops", … } ​​​ proto : Object { … } ​​ 1: {…} ​​​ data: Object { id: "11", name: "Professor X", … } 数据:对象{ID: “10”,名称: “独眼巨人”,...} :对象{...} 1:{...}数据:对象{ID: “11”,姓名:“ X教授”,…}

Use need to store the final below result to a js variable and you can use forEach to push the variable to another js array for example if the result is 使用need将下面的最终结果存储到js变量中,例如,如果结果为,则可以使用forEach将变量推入另一个js数组中

var a=[{data:{id:"10",name:"Cyclops"}},{data:{id:"11",name:"Professor X"}}]

then use 然后使用

var b=[]
a.forEach(x=>{b.push(x.data)});

it Will get the below result 它会得到以下结果

(2) [{…}, {…}] 0 : {id: "10", name: "Cyclops"} 1 : {id: "11", name: "Professor X"} length : 2 proto : Array(0) (2)[{…},{…}] 0:{id:“ 10”,名称:“ Cyclops”} 1:{id:“ 11”,名称:“ Professor X”}长度:2 proto :Array( 0)

I hope it will help you 希望对您有帮助

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

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