切换多维数组的顺序

时间:2013-03-13 13:05:07

标签: php

我有以下数组:

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

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

我怎样才能将其转换为:

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

    [2] => Array
        (
            [type] => customer
            [direction] => backward
        )

6 个答案:

答案 0 :(得分:4)

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

或者对于动态数量的键:

$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
    )
);

是的,我很喜欢阵列,谢谢你的提问。

答案 1 :(得分:1)

试试这个:

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

echo "<pre>";
print_r($res);

答案 2 :(得分:1)

以下代码可以满足您的需求。

$newar = array();
foreach($ar as $key=>$val) {
  foreach ($val as $k=>$v) {
    $newar[$k][$key] = $v;
  }
}
var_dump($newar);

答案 3 :(得分:0)

尝试:

$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;
  }
}

答案 4 :(得分:0)

这似乎可以解决问题:

  $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>';

答案 5 :(得分:0)

您正在寻找多维阵列翻转。我在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
?>

这应该做你想做的事我相信,但我还没有测试过。

相关问题