-4

How to merge only index of array itself, I have only one and I want to combine its index, I want to make 3d to 2d array. I just want to combine index of one array with in one index.This is one array,I am not asking for merge two different array.

Array
(
[0] => Array
    (
        [East] => 2
    )

[1] => Array
    (
        [North] => 2
    )

)

Now I want to format this array like below format.there can be nth no of indexes with in same array.

Array
(
[0] => Array
    (
        [East] => 2
        [North] => 2
    )

)
Aj Kosh
  • 47
  • 9
  • this is not duplicate.check there that is for two arrays i m asking single array and within the array. – Aj Kosh Dec 04 '15 at 15:34

1 Answers1

2
<?php

$a = array(0 => array('East' => 2), 1 => array('North' => 2));

$b[0] = $a[0];

for($i = 1; $i < count($a); $i++) {
  $key = array_keys($a[$i])[0];
  $b[0][$key] = $a[$i][$key];
}   

print_r($b);

?>

Output:

Array
(
[0] => Array
    (
        [East] => 2
        [North] => 2
    )

)
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63