Search code examples
phparraysmultidimensional-arraytranspose

How to arrange a multidimensional array in a certain format?


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


Solution

  • 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.