PHP将单个数组元素合并为唯一数组

时间:2012-08-23 15:50:34

标签: php arrays recursion

我不确定如何处理这个问题的标题,对不起,如果它有点令人困惑。

我有一个数组;

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

我似乎无法弄明白,数组太快结束,或者最终索引太多等等。想法?

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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阵列正在逐渐清空。如果您正在处理大量数据,有时会产生影响。如果数据集 REALLY 大,并且代码已经过全面测试,您甚至可能需要考虑源数组中的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/>';
}
  

栏包含:=&gt; ás,ás

     

quar包含:=&gt; 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, ž