1

Imagine that I have 2D array in PHP like this:

<?php
$db = array(
    "1" => array(
         "1" => "aaa",
         "2" => "bbb",
         "3" => "ccc"
    ),
    "2" => array(
         "1" => "ddd",
         "2" => "eee"
    ),
    "3" => array(
         "1" => "fff",
         "2" => "ggg",
         "3" => "hhh"
    )
);
?>

And I would like to convert it to JS, yes I can do something like:

var db = new Array(3);
db[1][1] = "aaa";
db[1][2] = "bbb";
db[1][3] = "ccc";
db[2][1] = "ddd";
db[2][2] = "eee";
db[3][1] = "fff";
db[3][2] = "ggg";
db[3][3] = "hhh";

Bit this initialization of array in JS is ugly, is there better way to initialize it like in PHP?

1
  • If you have it in PHP and want to convert (or transfer) it to Javascript you can use json_encode and JSON.parse. Commented Aug 29, 2018 at 14:26

4 Answers 4

1

js can also initialize array in one line :

var db = [["aaa", "bbb", "ccc"], ["ddd", "eee", "fff"], ["ggg", "hhh", "iii"]]

it is actually very rare to see the new Array() notation (I had forgotten about it until reading you question)

Bonus : this way of declaring arrays is also correct in php

$db = [["aaa", "bbb", "ccc"], ["ddd", "eee", "fff"], ["ggg", "hhh", "iii"]];
Sign up to request clarification or add additional context in comments.

Comments

0

You can json_encode the array

php

$db = array(
"1" => array(
     "1" => "aaa",
     "2" => "bbb",
     "3" => "ccc"
),
"2" => array(
     "1" => "ddd",
     "2" => "eee"
),
"3" => array(
     "1" => "fff",
     "2" => "ggg",
     "3" => "hhh"
)
);
echo json_encode($db);

output

 {"1":{"1":"aaa","2":"bbb","3":"ccc"},"2":{"1":"ddd","2":"eee"},
 "3": {"1":"fff","2":"ggg","3":"hhh"}}

Javascript

 var arr = {"1":{"1":"aaa","2":"bbb","3":"ccc"},"2":{"1":"ddd","2":"eee"},
 "3": {"1":"fff","2":"ggg","3":"hhh"}};

Output accessing some index

 arr[1][1] = aaa
 arr[1][3] = ccc
 arr[2][2] = eee

Comments

0

Your PHP array looks like it's an associative array of associative arrays: a great way to have named keys that point to your values.

In JS, arrays are numerically indexed from zero, and the data type to use for named associative key => value pairs is the JS object.

Here are two approaches. Example #1 uses the array syntax, if the index value does not need to be named explicitly. Example #2 demonstrates using a JS object, in case you need your keys to be named with a string.

var example1 = [
    ['aaa', 'bbb', 'ccc'],
    ['ddd', 'eee'],
    ['fff', 'ggg', 'hhh']
];


var example2 = {
    "1": {
        "1": "aaa",
        "2": "bbb",
        "3": "ccc"
    },            
    "2": {
        "1": "ddd",
        "2": "eee"
    },
    "3": {
        "1": "fff",
        "2": "ggg",
        "3": "hhh"
    }
};

Comments

0

If you want to use a PHP array on JS Script, you can declare it on PHP

<?php 
  $db = array(
  "1" => array(
      "1" => "aaa",
      "2" => "bbb",
      "3" => "ccc"
  ),
  "2" => array(
      "1" => "ddd",
      "2" => "eee"
  ),
  "3" => array(
      "1" => "fff",
      "2" => "ggg",
      "3" => "hhh"
  )
); 
?>

then just "echo" it on JS array declaration.

<script>
    var myJsObject = <?= json_encode($db) ?>; 
</script>

Otherwise if your question is about coding syle

<script> 
 var myJsObject = { 
              "1" : { 
                  "1" : "aaa", 
                  "2": "bbb", 
                  "3" : "ccc"
              }, 
              "2" : { 
                  "1" : "dd", 
                  "2": "ee"
               }
             }; 
</script>

Note that PHP Associative Arrays are Objects if represented in JS, not arrays. You can access them as array too.

Comments

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.