数组第一个值作为新数组的键,第二个值作为其值

时间:2012-02-29 11:19:49

标签: php arrays

我有一个数组:

 $array1 =  array
      0 => 
        array
          0 => string 'biodata' (length=7)
          1 => string 'family_name' (length=11)
      1 => 
        array
          0 => string 'biodata' (length=7)
          1 => string 'first_name' (length=10)
      2 => 
        array
          0 => string 'biodata_education' (length=17)
          1 => string 'subject' (length=7)
      3 => 
        array
          0 => string 'biodata_education' (length=20)
          1 => string 'year' (length=5)

需要转换为:

array
  biodata => 
    array
      0 => string 'family_name' (length=7)
      1 => string 'first_name' (length=11)
  biodata_education => 
    array
      0 => string 'subject' (length=7)
      1 => string 'year' (length=10)

因为它可以通过简单的迭代完成,我尝试了这个并完成了。

foreach($array1 as $tbl):
            $table[$tbl[0]][] = $tbl[1];

        endforeach;

1 个答案:

答案 0 :(得分:1)

<?php

//map the array using a foreach loop    
foreach($array1 as $tbl)
{
    $table[ $tbl[0] ][] = $tbl[1];
}