PHP数组,将键和值移动到新数组,但将其混合

时间:2016-05-13 21:06:32

标签: php arrays

所以我需要转换一个来自表单帖子的PHP数组,以便我可以在数据库中更有用地使用它。

Array
(
  [first_name] => Array
    (
        [0] => Ben
        [1] => Tom
        [2] => Sarah
    )

  [last_name] => Array
    (
        [0] => Wills
        [1] => Main
        [2] => Bliss
    )

[email] => Array
    (
        [0] => ben.wills@argh.com
        [1] => tommain@argh.com
        [2] => sbliss@argh.com
    )
 )

为:

Array
(
[0] => Array
    (
        [first_name] => Ben
        [last_name] => Wills
        [email] => ben.wills@argh.com
    )

[1] => Array
    (
        [first_name] => Tom
        [last_name] => Main
        [email] => tommain@argh.com
    )
[2] => Array
    (
        [first_name] => Sarah
        [last_name] => Bliss
        [email] => sbliss@argh.com
    )

 )

最糟糕的是,我不知道从哪里开始!

提前致谢:)

3 个答案:

答案 0 :(得分:2)

使用array_keysarray_valuesarray_mapcall_user_func_arrayarray_combine函数的解决方案:

$keys = array_keys($arr);  // supposing $arr is your initial array
$data = call_user_func_array("array_map", array_merge([null], array_values($arr)));
$result = array_map(function($v) use($keys){
    return array_combine($keys, $v);
}, $data);

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [first_name] => Ben
            [last_name] => Wills
            [email] => ben.wills@argh.com
        )
    [1] => Array
        (
            [first_name] => Tom
            [last_name] => Main
            [email] => tommain@argh.com
        )
    [2] => Array
        (
            [first_name] => Sarah
            [last_name] => Bliss
            [email] => sbliss@argh.com
        )
)

答案 1 :(得分:1)

使用以下代码。希望至少这会给出一些想法如何继续:)

$array = array(
  'first_name' => array('Ben','Tom','Sarah'),
  'last_name' => array('Wills','Main','Bliss'),
  'email' => array('ben.wills@argh.com','tommain@argh.com','sbliss@argh.com')
 );
  // loop the array
  foreach($array as $key=>$value){
      foreach($value as $k=>$v){
          // use the first loop key here
          $new_array[$k][$key] = $v;
      }
  }
  print_r($new_array);

Out Put:

Array
(
[0] => Array
    (
        [first_name] => Ben
        [last_name] => Wills
        [email] => ben.wills@argh.com
    )

[1] => Array
    (
        [first_name] => Tom
        [last_name] => Main
        [email] => tommain@argh.com
    )

[2] => Array
    (
        [first_name] => Sarah
        [last_name] => Bliss
        [email] => sbliss@argh.com
    )

答案 2 :(得分:0)

使用PHP的MultipleIterator

可以非常轻松地保留关键名称进行转换
$data = array(
    'first_name' => array(
        0 => 'Ben',
        1 => 'Tom',
        2 => 'Sarah',
    ),
    'last_name' => array(
        0 => 'Wills',
        1 => 'Main',
        2 => 'Bliss',
    ),
    'email' => array(
        0 => 'ben.wills@argh.com',
        1 => 'tommain@argh.com',
        2 => 'sbliss@argh.com',
    ),
);

$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
foreach($data as $key => $column) {
    $mi->attachIterator(new ArrayIterator($column), $key);
}
$newData = [];
foreach($mi as $row) {
    $newData[] = $row;
}

var_dump($newData);

Demo