PHP数组索引到数组关联

时间:2016-03-07 11:33:57

标签: php arrays

我在PHP中得到了一个数组数组,如下所示:

array (size=3)
  0 => 
    array (size=3)
      0 => string 'abc' (length=3)
      1 => string 'def' (length=3)
      2 => string 'ghi' (length=3)
  1 => 
    array (size=3)
      0 => string '01234' (length=5)
      1 => string '01234' (length=5)
      2 => string '01234' (length=5)
  2 => 
    array (size=3)
      0 => string '98765' (length=5)
      1 => string '98765' (length=5)
      2 => string '98765' (length=5)

现在我希望第一个数组成为父数组其余部分的一个assosiative数组的键,或kind

array (size=2)
  0 => 
    array (size=3)
      'abc' => string '01234' (length=5)
      'def' => string '01234' (length=5)
      'ghi' => string '01234' (length=5)
  1 => 
    array (size=3)
      'abc' => string '98765' (length=5)
      'def' => string '98765' (length=5)
      'ghi' => string '98765' (length=5)

编辑:但我只能得到这样的第一个数组来定义标题:

$header = reset($tabOfTabs);

1 个答案:

答案 0 :(得分:2)

你可以试试这个 -

$indexes = array_shift($your_array); // pop out the first array to set the indexes

foreach($your_array as $key => $array) {
    $your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
} 

Demo