PHP array, move keys and values to new array, but mix it up



PHP Snippet 1:

$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);

PHP Snippet 2:

Array
(
    [0] => Array
        (
            [first_name] => Ben
            [last_name] => Wills
            [email] => [email protected]
        )
    [1] => Array
        (
            [first_name] => Tom
            [last_name] => Main
            [email] => [email protected]
        )
    [2] => Array
        (
            [first_name] => Sarah
            [last_name] => Bliss
            [email] => [email protected]
        )
)

PHP Snippet 3:

$array = array(
  'first_name' => array('Ben','Tom','Sarah'),
  'last_name' => array('Wills','Main','Bliss'),
  'email' => array('[email protected]','[email protected]','[email protected]')
 );
  // 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);

PHP Snippet 4:

Array
(
[0] => Array
    (
        [first_name] => Ben
        [last_name] => Wills
        [email] => [email protected]
    )

[1] => Array
    (
        [first_name] => Tom
        [last_name] => Main
        [email] => [email protected]
    )

[2] => Array
    (
        [first_name] => Sarah
        [last_name] => Bliss
        [email] => [email protected]
    )

PHP Snippet 5:

$data = array(
    'first_name' => array(
        0 => 'Ben',
        1 => 'Tom',
        2 => 'Sarah',
    ),
    'last_name' => array(
        0 => 'Wills',
        1 => 'Main',
        2 => 'Bliss',
    ),
    'email' => array(
        0 => '[email protected]',
        1 => '[email protected]',
        2 => '[email protected]',
    ),
);

$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);