0

I have the following array:

Array
(
    [type] => Array
        (
            [1] => default
            [2] => customer
        )

    [direction] => Array
        (
            [1] => forward
            [2] => backward
        )

How could I convert it into:

Array
(
    [1] => Array
        (
            [type] => default
            [direction] => forward
        )

    [2] => Array
        (
            [type] => customer
            [direction] => backward
        )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
  • 1
    Not my dv, but it doesn't show any attempt to solve the problem and more importantly it's written in a way that makes it unlikely that others will find and benefit from it in the future. So it will help *you*, but not the community. – Jon Mar 13 '13 at 13:10

6 Answers6

4
$array = array_map(
    function ($type, $direction) { return compact('type', 'direction'); },
    $array['type'],
    $array['direction']
);

Or for dynamic numbers of keys:

$array = call_user_func_array(
    'array_map',
    array_merge(
        array(function () use ($array) {
            $values = func_get_args();
            return array_combine(array_keys($array), $values);
        }),
        $array
    )
);

Yeah, I'm having fun with arrays, thanks for asking.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

Try this :

$res     = array();
foreach($your_array as $key=>$val){
   foreach($val as $k=>$v){
      $res[$k][$key]  = $v;
   }
}

echo "<pre>";
print_r($res);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
1

The code below will do what you want.

$newar = array();
foreach($ar as $key=>$val) {
  foreach ($val as $k=>$v) {
    $newar[$k][$key] = $v;
  }
}
var_dump($newar);
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
0

Try with:

$input  = array( /* your data */ );
$output = array();

foreach ( $input as $key => $data ) {
  foreach ( $data as $k => $v ) {
    if ( !isset($output[$k]) ) {
      $output[$k] = array();
    }
    $output[$k][$key] = $v;
  }
}
hsz
  • 148,279
  • 62
  • 259
  • 315
  • `if ( !isset($output[$k]) ) { $output[$k] = array(); }` is not necessary because PHP permits child arrays to be pushed without explicitly initializing their parent when using square brace syntax. – mickmackusa Sep 02 '22 at 07:01
0

This seems to do the trick:

  $array = array(

    'foo' => array(

      1 => 'default',
      2 => 'customer',

    ),

    'foo2' => array(

      1 => 'forward',
      2 => 'backward',

    ),

  );

  $new_array = array();

  foreach ($array as $key => $value)
  {

    foreach ($value as $k => $v)
    {

      if (!isset($new_array[$k]))
      {
        $new_array[$k] = array();
      }

      $new_array[$k][$key] = $v;

    }

  }

  echo '<pre>' . print_r($new_array, true). '</pre>';
Michael
  • 11,912
  • 6
  • 49
  • 64
  • `if (!isset($new_array[$k])) { $new_array[$k] = array(); }` is not necessary because PHP permits child arrays to be pushed without explicitly initializing their parent when using square brace syntax. – mickmackusa Sep 02 '22 at 07:02
0

You are looking for a multidimensional array flip. I found this code off php.net

<?php
function multi_array_flip($arrayIn, $DesiredKey, $DesiredKey2=false, $OrigKeyName=false) {
$ArrayOut=array();
foreach ($arrayIn as $Key=>$Value)
    {
        // If there is an original key that need to be preserved as data in the new array then do that if requested ($OrigKeyName=true)
        if ($OrigKeyName) $Value[$OrigKeyName]=$Key;
        // Require a string value in the data part of the array that is keyed to $DesiredKey
        if (!is_string($Value[$DesiredKey])) return false;

        // If $DesiredKey2 was specified then assume a multidimensional array is desired and build it
        if (is_string($DesiredKey2))
        {
            // Require a string value in the data part of the array that is keyed to $DesiredKey2
            if (!is_string($Value[$DesiredKey2])) return false;

            // Build NEW multidimensional array
            $ArrayOut[$Value[$DesiredKey]][$Value[$DesiredKey2]]=$Value;
        }

            // Build NEW single dimention array
        else $ArrayOut[$Value[$DesiredKey]][]=$Value;
    }
return $ArrayOut;
}//end multi_array_flip
?>

This should do what you want to do I believe but I have not tested it.

Bil1
  • 440
  • 2
  • 18