0

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.

2
  • In json [] are arrays, {} are objects. So you'd need to create an array, with an object with those values. Commented Sep 17, 2016 at 22:57
  • Your brackets look a bit unbalanced at the beginning and end of your example JSON. Commented Sep 17, 2016 at 23:05

1 Answer 1

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"]}]
Sign up to request clarification or add additional context in comments.

2 Comments

This is what i need
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.