0

我不确定如何处理这个问题的标题,如果有点混乱,很抱歉。

我有一个数组;

  Array
  (
      [username] => Array
          (
              [0] => 'a'
              [1] => 'b'
              [2] => 'c'
              [3] => 'd'
              [4] => 'e'
              [5] => 'f'
          )

      [email] => Array
          (
              [0] => 
              [1] => 
              [2] => 
              [3] => 
              [4] => 
              [5] => 
          )

      [level] => Array
          (
              [0] => 1
              [1] => 1
              [2] => 1
              [3] => 1
              [4] => 1
              [5] => 1
          )

      [role] => Array
          (
              [0] => 2
              [1] => 1
              [2] => 1
              [3] => 1
              [4] => 2
              [5] => 1
          )

      [password] => Array
          (
              [0] => 
              [1] => 
              [2] => 
              [3] => 
              [4] => 
              [5] => 
          )

      [id] => Array
          (
              [0] => 1
              [1] => 2
              [2] => 3
              [3] => 4
              [4] => 5
              [5] => 6
          )

  )

但我想要这种格式:

  Array
  (
      [0] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )
      [1] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )
      [2] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )
      [3] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )
      [4] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )
      [5] => Array
          (
              [username] => 'a'
              [id] => 'a'
              [email] => 'a'
          )

我似乎无法弄清楚,数组结束得太快,或者索引太多等等。想法?

4

2 回答 2

1

我会遍历数组并像这样重建它:

$index_count = count($array['username']); // in your case, this is 6

$new_array = array();
for ($i = 0; $i < $index_count; $i++) {
    $new_array[] = array(
        'username' => $array['username'][$i],
        'email' => $array['email'][$i],
        'id' => $array['id'][$i]
    );
}

更新如果您希望这考虑到任何和所有可能的键,请尝试以下操作:

$keys = array_keys($array);
if (count($keys)) {
    $index_count = count($array[$keys[0]]);

    $myArray = array();
    for ($i = 0; $i < $index_count; $i++) {
        $temp = array();
        foreach($keys as $key) {
            $temp[$key] = $array[$key][$i];
        }
        $myArray[] = $temp;
    }
}
于 2012-08-23T15:58:17.880 回答
1

一个不同的看法,但这是我要做的 - 一步一步:

首先,让我们拿到钥匙——我们会非常需要它们:

$result = array();//<-- this is the result array
$keys = array_keys($array);

然后,我会得到一个空的模板数组(有点像模型对象)

$base = array_fill_keys($keys,array());
//array('username'=>array,'id'=>array(),... 

然后,使用它来构建结果数组:

$result = array_fill(0,count($array['username']),$base);
//In one-liner format, it looks like this:
$result = array_fill(0,count($array['username']),array_fill_keys(array_keys($array),array()));
//Works, but is messy as f***

然后只需填写很多,这就是该$keys变量得到回报的地方:

$length = count($result);
while ($key = array_shift($keys))
{
    for ($i=0;$i<$length;$i++)
    {
        $result[$i][$key] = $array[$key][$i];
    }
}

请注意,我更喜欢使用 while 循环,因为它更干净并且(略微)更快。更清洁,因为$keys随着您的前进,该阵列正在被清空。如果您正在处理大量数据,它有时会产生影响。如果数据集真的很大,并且代码已经过彻底测试,您甚至可能需要shifting从源数组中考虑(因为它包含所有数据,它比只包含键的数组大得多):

while ($vals = array_shift($array))
{
    $key = array_shift($keys);//<-- keep track of what array has been shifted
    for ($i=0;$i<$length;$i++)
    {
        $result[$i][$key] = $vals[$i];
    }
}

这巧妙地清理了源数组和键。我已经在我的服务器 writecodeonline 和 codepad 上测试了最后一种方法,结果完全相同:

$foo = array('bar'=>array_fill(0,2,'ás'),'quar'=>range('a','z'));
$keys = array_keys($foo);
while($vals = array_shift($foo))
{
    $key = array_shift($keys);
    echo $key.' contains: => '.implode(', ',$vals).'<br/>';
}

栏包含:=> ás, ás

quar 包含:=> a、b、c、d、e、f、g、h、i、j、k、l、m、n、o、p、q、r、s、t、u、v、w , x, y, z

于 2012-08-23T16:40:51.657 回答