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'}
];
asked Dec 18, 2020 at 16:44
Reham Fahmy
5,07316 gold badges58 silver badges74 bronze badges
2 Answers 2
var myarr = [
{ 'one' : ['1one','2one','3one']},
'two',
'three',
{'four' : ['1four','2four']},
'five'
];
answered Dec 18, 2020 at 16:52
Neil W
9,4955 gold badges37 silver badges57 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Teemu
The JSON conversion PHP makes, differs:
{"one":["1one","2one","3one"],"0":"two","1":"three","four":["1four","2four"],"2":"five"}.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' : []}
];
answered Dec 18, 2020 at 16:54
Parikshith Kedilaya M
2311 silver badge8 bronze badges
1 Comment
Justinas
two is value, not keydefault
echo json_encode($myarr);