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',
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]
]}
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.
asked Sep 17, 2016 at 22:51
Raffy T Lawrence
3631 gold badge6 silver badges19 bronze badges
1 Answer 1
$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"]}]
answered Sep 17, 2016 at 23:00
Blake
2,3241 gold badge16 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Raffy T Lawrence
This is what i need
Blake
Glad to help, but this probably could have been solved by doing some research on understanding JSON a bit more. Feel free to mark it as the answer if it solves your issue, though.
default
[]are arrays,{}are objects. So you'd need to create an array, with an object with those values.