1

I need to convert a PHP array that I'm getting from a form submission, 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] => ben.wills@argh.com
        [1] => tommain@argh.com
        [2] => sbliss@argh.com
    )
 )

to:

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
    )

 )

How can I change the values' key paths so that the first level keys and the second level keys are swapped?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ben
  • 53
  • 7

3 Answers3

2

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] => 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
        )
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Nice solution; I looked at transposition, but couldn't think how to restore the associative keys – Mark Baker May 13 '16 at 21:27
  • @MarkBaker, I remembered that interesting feature of `array_map` : "... construct an array of arrays, which can be easily performed by using NULL as the name of the callback function " and that was the "thing" – RomanPerekhrest May 13 '16 at 21:37
1

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('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
    )

)

Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
0

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 => '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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385