PHP : 分段合并多个数组

PHP : merge multi arrays in sections

我从 .xlsx 加载数据sheet

我把它转换成两个数组

第一个是 header 列

第二个是数据列

我做到了

$c = array_combine($headers, $data);

when i print $c 

    Array
    (
    [id] => Array
        (
            [0] => 1
            [1] => 2
        )

    [f-name] => Array
        (
            [0] => Mohammed
            [1] => Ziad
        )

    [s-name] => Array
        (
            [0] => Amer
            [1] => Mohammed
        )

    [t-name] => Array
        (
            [0] => Hendy
            [1] => Shokry
        )
    [cid] => Array
        (
            [0] => 89
            [1] => 55   
        )
     )

我想要这样的结果

array(
[0] => Array(
        [id] => 0
        [f-name] => mohammed
        [s-name] => amer
        [t-name] => hendy
        [cid] => 89 
)
[1] => Array(
        [id] => 1
        [f-name] => ziad
        [s-name] => mohammed
        [t-name] => shokry
        [cid] => 55
)
)

尝试这样的事情:

for ($index = 0; &index < 2; ++$index)
{
  thing['id']     = $index;
  thing['f-name'] = $c['f-name'];
  thing['l-name'] = $c['s-name'];
  thing['t-name'] = $c['t-name'];
  thing['cid']    = $c['cid'];

  $newArra y[$index] = thing;
}

试试这个:

$data = array(
'id' => array
    (
        0 => 1,
        1 => 2
    ),
'f-name' => array
    (
        0 => 'Mohammed',
        1 => 'Ziad'
    ),
's-name' => array
    (
        0 => 'Amer',
        1 => 'Mohammed'
    ),
't-name' => array
    (
        0 => 'Hendy',
        1 => 'Shokry'
    ),
'cid' => array
    (
        0 => 89,
        1 => 55   
    )
);
//
$result = array();
foreach($data as $k=>$v){
    for($i=0;$i<count($data['id']);$i++){
        //$v[$i] = ($k!='id') ?: $i;// uncomment for reset id from 0
        $result[$i][$k] = $v[$i]; 
    }
}
var_dump($result);

结果:

array (size=2)
  0 => 
    array (size=5)
      'id' => int 1
      'f-name' => string 'Mohammed' (length=8)
      's-name' => string 'Amer' (length=4)
      't-name' => string 'Hendy' (length=5)
      'cid' => int 89
  1 => 
    array (size=5)
      'id' => int 2
      'f-name' => string 'Ziad' (length=4)
      's-name' => string 'Mohammed' (length=8)
      't-name' => string 'Shokry' (length=6)
      'cid' => int 55

只要有机会,我就喜欢包含 "fancy" 数组函数,例如 array_walk 和 array_map:

初始化您的数据

$data = [
  'id' => [
    0 => 1,
    1 => 2
  ],
  'f-name' => [
    0 => 'Mohammed',
    1 => 'Ziad'
  ],
  's-name' => [
    0 => 'Amer',
    1 => 'Mohammed'
  ],
  't-name' => [
    0 => 'Hendy',
    1 => 'Shokry'
  ],
  'cid' => [
    0 => 89,
    1 => 55   
  ]
];

转换数据

$result = [];
array_walk($data['id'], 
  function($f, $i) use ($data, &$result){
    $result[$i] = array_map(function($e) use ($i){
      return $e[$i];
    }, $data);
  }
);

输出结果

var_dump($result);

array(2) {
  [0]=>
  array(5) {
    ["id"]=>
    int(1)
    ["f-name"]=>
    string(8) "Mohammed"
    ["s-name"]=>
    string(4) "Amer"
    ["t-name"]=>
    string(5) "Hendy"
    ["cid"]=>
    int(89)
  }
  [1]=>
  array(5) {
    ["id"]=>
    int(2)
    ["f-name"]=>
    string(4) "Ziad"
    ["s-name"]=>
    string(8) "Mohammed"
    ["t-name"]=>
    string(6) "Shokry"
    ["cid"]=>
    int(55)
  }
}