3

I have a array in below format

Array
(
    [title] => Array
        (
            [0] => Ram 
            [1] => ramesh
            [2] => john
            [3] => mahesh

        )

    [neighborhood] => Array
        (
            [0] => delhi
            [1] => mumbai
            [2] => odisha
            [3] => banglore

        )



    [phone_no] => Array
        (
            [0] =>  9438648-4256
            [1] => 9438333-3390
            [2] => 9438771-0888
            [3] => 9438504-7000

        )
)

but i want in such format given below

Array
(
    [0] => Array
        (
            [title] => Ram 
            [neighborhood] => delhi
            [phone_no] =>  9438648-4256

        )

        [1] => Array
        (
            [title] => Ramesh 
            [neighborhood] => mumbai
            [phone_no] => 9438333-3390 

        )


        [2] => Array
        (
            [title] => john 
            [neighborhood] => odisha
            [phone_no] =>  9438771-0888

        )

        [3] => Array
        (
            [title] => mahesh 
            [neighborhood] => banglore
            [phone_no] => 9438504-7000

        )
}

I want the the array in above format in php.Any help is highly appreciated . Thanks in advance

d-_-b
  • 21,536
  • 40
  • 150
  • 256
prabhat kumar jena
  • 12,819
  • 1
  • 10
  • 10

2 Answers2

1
  $arr = array(
    "title" => array("t1","t2","t3","t4"),
    "neighborhood" => array("n1","n2","n3","n4"),
    "phone_no" => array("p1","p2","p3","p4"),

);

$keys = array_keys($arr);
$child_len = count($arr[ $keys[0] ]);

$returnArr = array();

for ($i=0; $i < $child_len; $i++) { 
    $returnArr[$i] = array();
    foreach ($keys as $key) {
        $returnArr[$i][$key] = $arr[$key][$i];
    }

}
var_dump($returnArr);

not the most effective , but will work for you

out would be like

[0]=>
  array(3) {
    ["title"]=>
    string(2) "t1"
    ["neighborhood"]=>
    string(2) "n1"
    ["phone_no"]=>
    string(2) "p1"
  }
  [1]=>
  array(3) {
    ["title"]=>
    string(2) "t2"
    ["neighborhood"]=>
    string(2) "n2"
    ["phone_no"]=>
    string(2) "p2"
  }
  [2]=>
  array(3) {
    ["title"]=>
    string(2) "t3"
    ["neighborhood"]=>
    string(2) "n3"
    ["phone_no"]=>
    string(2) "p3"
  }
  [3]=>
  array(3) {
    ["title"]=>
    string(2) "t4"
    ["neighborhood"]=>
    string(2) "n4"
    ["phone_no"]=>
    string(2) "p4"
  }
0

array_combine() and array_column() make clear code for this task. The for loop will iterate for each column in the multi-dimensional array. Then simply pair-up the array keys with the column values using array_combine().

Code: (Demo)

$array=[
    'title'=>['Ram','ramesh','john','mahesh'],
    'neighborhood'=>['delhi','mumbai','odisha','banglore'],
    'phone_no'=>['9438648-4256','9438333-3390','9438771-0888','9438504-7000']
];

$keys=array_keys($array);  // cache the array keys
$columns=sizeof($array['title']); // cache the number of columns to iterate
for($c=0; $c<$columns; ++$c){
    $result[]=array_combine($keys,array_column($array,$c));
}

var_export($result);

Output:

array (
  0 => 
  array (
    'title' => 'Ram',
    'neighborhood' => 'delhi',
    'phone_no' => '9438648-4256',
  ),
  1 => 
  array (
    'title' => 'ramesh',
    'neighborhood' => 'mumbai',
    'phone_no' => '9438333-3390',
  ),
  2 => 
  array (
    'title' => 'john',
    'neighborhood' => 'odisha',
    'phone_no' => '9438771-0888',
  ),
  3 => 
  array (
    'title' => 'mahesh',
    'neighborhood' => 'banglore',
    'phone_no' => '9438504-7000',
  ),
)

Alternatively, you could use a foreach loop like this:

$keys=array_keys($array);  // cache the array keys
foreach($array['title'] as $i=>$v){
    $result[]=array_combine($keys,array_column($array,$i));
}

$v is declared but not used so that $i can be declared.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136