HOW TO CONVERT TO ARRAY code example

Example 1: javascript object toarray

var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map(function(key) {
  return [Number(key), obj[key]];
});

console.log(result);

Example 2: javascript convert to array

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

Example 3: js convert obj to array

const array = [
  ['key', 1],
  ['two', 2],
];

Object.fromEntries(array);
// { one: 1, two: 2 }

Example 4: convert array to array

new_images = array();
// format image before upload
for ($i = 0; $i < count($images['name']); $i++) {
  $new_images[] = array(
    'name' 		=> $images['name'][$i],
    'type' 		=> $images['type'][$i],
    'tmp_name' 	=> $images['tmp_name'][$i],
    'error' 	=> $images['error'][$i],
    'size' 		=> $images['size'][$i],
  );
}

<pre>Array
(
    [name] => Array  (
            [0] => Screen Shot 2020-10-21 at 10.44.30 AM.png
            [1] => Screen Shot 2020-10-21 at 9.56.12 AM.png )
    [type] => Array (
            [0] => image/png
            [1] => image/png )
    [tmp_name] => Array   (
            [0] => /Applications/MAMP/tmp/php/phpnlVcZU
            [1] => /Applications/MAMP/tmp/php/php1qaHkj  )
    [error] => Array (
            [0] => 0
            [1] => 0 )
    [size] => Array  (
            [0] => 61923
            [1] => 62194)

)
</pre>

// to 
<pre>Array
(
    [0] => Array (
            [name] => Screen Shot 2020-10-21 at 10.44.30 AM.png
            [type] => image/png
            [tmp_name] => /Applications/MAMP/tmp/php/phpJz6xqI
            [error] => 0
            [size] => 61923 )
    [1] => Array  (
            [name] => Screen Shot 2020-10-21 at 9.56.12 AM.png
            [type] => image/png
            [tmp_name] => /Applications/MAMP/tmp/php/phpHSBXaI
            [error] => 0
            [size] => 62194  )
)
</pre>

Tags:

Java Example