0

I searched and I couldn't find any related answer so far. So excuse me if I am mistaken or its a basic question.

I have this array:

Array
(
    [name] => Array
        (
            [0] => name1
            [1] => name2
            [2] => name3
        )

    [price] => Array
        (
            [0] => price1
            [1] => price2
            [2] => price3
        )

    [option] => Array
        (
            [0] => option1
            [1] => option2
            [2] => option3
        )

    [disabled] => Array
        (
            [0] => disabled1
            [1] => disabled2
            [2] => disabled3
        )

)

my question is, how can I get the following array from this.

    Array
    (
        [0] => Array
            (
                [name] => name1
                [price] => price1
                [option] => option1
                [disabled] => disabled1
            )
        [1] => Array
            (
                [name] => name2
                [price] => price2
                [option] => option2
                [disabled] => disabled2
            )    
.
.
.
    )

Thank you. :)

Amir Soleymani
  • 355
  • 1
  • 3
  • 9

2 Answers2

1

your input array is $in_arr

$out_arr = array();
foreach ($in_arr as $key => $sub_arr)
{
    foreach ($sub_arr as $sub_key => $sub_value)
    {
         $out_arr[$sub_key][$key] = $sub_value;
    }
}

print_r($out_arr);

your required output array is $out_arr

Jahanzeb
  • 613
  • 4
  • 11
0

Suppose your input array is $arr1.

<?php
$v = array();
for ($i=0; i<3; $i++)
{
   $v[i] = array();
   foreach ($arr1 as $x => $y)
   $v[i][$x] = $y[i]
}
?>

$v is this output array.

kelsar
  • 108
  • 5