Raffy T Lawrence Raffy T Lawrence - 1 year ago 58
Javascript Question

How to create nested array

I need to build an array look like this, that when you JSON.stringify in javascript it will look similar to this. How, please help

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

How is the right way to do it?

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

print_r $arr;

Also, I did this on my other part of code but its not nested. I cannot do it again because of the comma inside of the inner array is hard to trim out.

Answer Source
$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"]}]