0

我有以下数组:

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
        )
4

6 回答 6

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

是的,我很喜欢数组,谢谢你的提问。

于 2013-03-13T13:10:21.587 回答
1

尝试这个 :

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

echo "<pre>";
print_r($res);
于 2013-03-13T13:08:25.377 回答
1

下面的代码将做你想做的事。

$newar = array();
foreach($ar as $key=>$val) {
  foreach ($val as $k=>$v) {
    $newar[$k][$key] = $v;
  }
}
var_dump($newar);
于 2013-03-13T13:08:36.043 回答
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;
  }
}
于 2013-03-13T13:08:59.493 回答
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>';
于 2013-03-13T13:09:30.973 回答
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
?>

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

于 2013-03-13T13:12:27.577 回答