1

I've the following PHP array (key value pair multi dimensional array)

$myarr = array (
'one' => array ('1one','2one','3one'),
'two',
'three',
'four' => array ('1four','2four'),
'five'
);

How to writ exact array using JAVASCRIPT ?

I'm not good in javascript but this is my failure try, i do not know if javascript support such type of arrays or not!

var myarr = [
{ 'one' : ['1one','2one','3one']},
{'two'},
{'three'},
{'four' : ['1four','2four']},
{'five'}
];
3
  • 7
    echo json_encode($myarr); Commented Dec 18, 2020 at 16:47
  • 2
    sandbox.onlinephpfunctions.com/code/… Commented Dec 18, 2020 at 16:49
  • 1
    When you have anything in {} , then it has to be a key value pair, ,but inside [] you only have idexes, but every index you can nest more things. Commented Dec 18, 2020 at 16:51

2 Answers 2

3
var myarr = [
{ 'one' : ['1one','2one','3one']},
'two',
'three',
{'four' : ['1four','2four']},
'five'
];
Sign up to request clarification or add additional context in comments.

1 Comment

The JSON conversion PHP makes, differs: {"one":["1one","2one","3one"],"0":"two","1":"three","four":["1four","2four"],"2":"five"}.
-1

You can do this to keep things consistent in a key-value pair:

var myarr = [
  {'one' : ['1one','2one','3one']},
  {'two' : []},
  {'three' : []},
  {'four' : ['1four','2four']},
  {'five' : []}
];

1 Comment

two is value, not key

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.