如何创建嵌套数组

时间:2016-09-17 22:51:51

标签: javascript php arrays

我需要构建一个这样的数组,当你在javascript中使用JSON.stringify时,它看起来与此类似。如何,请帮助

     [{
      name: 'Microsoft Internet Explorer',
      id: 'Microsoft Internet Explorer',
      data:[
             ['v11.0', 24.13],
             ['v8.0', 17.2],
             ['v9.0', 8.11],
             ['v10.0', 5.33],
             ['v6.0', 1.06],
             ['v7.0', 0.5] 
     ]}

如何正确地做到这一点?

$arr[] = array('name' => 'example', 'y' => 25, 'drill' => 'test', 'data :' => 
'["test" =>25]'  );

print_r $arr;

另外,我在代码的其他部分做了这个,但它没有嵌套。我不能再这样做了,因为内部数组里面的逗号很难被删除。

1 个答案:

答案 0 :(得分:3)

$array = []; 

$test = new stdClass();
$test->name = "Name";
$test->id = "Id";
$test->data = array("val1", "val2", "val3");

$array[] = $test;

echo json_encode($array); // [{"name":"Name","id":"Id","data":["val1","val2","val3"]}]