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

so I need to convert a PHP array i'm getting from a form post, so that I can use it more usefully in a db.

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]
    )
 )

to:

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]
    )

 )

And the worst part is I don't event know where to start!

Thanks in advance :)



Solution 1:[1]

The solution using array_keys, array_values, array_map, call_user_func_array and array_combine functions:

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

The output:

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]
        )
)

Solution 2:[2]

Use the below code. Hope at least this gives some idea how to proceed :)

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

Out Put:

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]
    )

)

Solution 3:[3]

Doing a transform while retaining the key names can be achieved quite easily using PHP's MultipleIterator

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

Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RomanPerekhrest
Solution 2 Ravinder Reddy
Solution 3 Mark Baker