2

I have this PHP array:

Array(
    [0] => Array(
      ["Import"]=> Array
          (
              [0] => 1.00
              [1] => 2.00
              [2] => 1.00
              [3] => 9.00
          )

      ["Page"] => Array
          (
              [0] => 1
              [1] => 4
              [2] => 5
              [3] => 6
          )

      ["Key"] => Array
          (
              [0] => 1
              [1] => 22
              [2] => 88
              [3] => 3
          )
    )
)

I need to get:

Array(
  [0] => Array(
    [0] => Array(
      ["Import"] => 1.00
      ["Page"] => 1
      ["Key"] => 1
    )
    [1] => Array(
      ["Import"] => 2.00
      ["Page"] => 4
      ["Key"] => 22
    )
    [2] => Array(
      ["Import"] => 1.00
      ["Page"] => 5
      ["Key"] => 88
    )
    [3] => Array(
      ["Import"] => 9.00
      ["Page"] => 6
      ["Key"] => 3
    )
  )
)

I've checked array_merge and array_combine but I can't find a way to use them in this case.
How can I do?

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160

3 Answers3

2

Try this. Seems to work as you expect.

<?php
$source = array(
    array(
        'Imports' => array(
        1.00,2.00,1.00, 9.00),
        'Page' => array(
        1,4,5,6),
        'Key' => array(
        1,22,88,3)
    )
);

print_r($source);
$dest = array();
foreach($source as $key => $src) {
    foreach($src as $typeKey => $typeArr) {
        foreach($typeArr as $index => $val){
            $dest[$key][$index][$typeKey] = $val;
        }   
    }
}

print_r($dest);
?>

Here is a demo: http://codepad.org/bht4ne7K

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
1

In PHP 5.5 you can easily achieve that with array_column() function:

$array = [
'Imports' => ['i0', 'i1'],
'Page'    => ['p0', 'p1'],
'Key'     => ['k0', 'k1']
];


$i = 0;
$result = array_map(function($x) use ($array, &$i)
{
   return array_combine(array_keys($array), array_column($array, $i++));
}, current($array));
//var_dump($result);

-but for earlier versions you'll need to gather your array with foreach like:

$result = [];
foreach($array as $key=>$item)
{
   foreach($item as $i=>$value)
   {
      $result[$i][$key] = $value;
   }
}
//var_dump($result);

-you'll need, of course, do that with each element of your parent array (code above is a sample of how to rearrange 2-level array). That, again, can be easily done with array_map()

Alma Do
  • 37,009
  • 9
  • 76
  • 105
1

Something like this:

function switchKeys(Array $input) {
    $result = array();
    foreach ($input as $field => $data) {
        if (is_array($data)) {
            foreach ($data as $index => $value) {
                $result[$index][$field] = $value;
            }
        }
    }
    return $result;
}

$input = array(
    "imports" => array(1.00, 2.00, 1.00, 9.00,),
    "page" => array(1, 4, 5, 6,),
    "key" => array(1, 22, 88, 3,),
);

$output = switchKeys($input);

var_export($input);
var_export($output);

Note that your input array has one more level, so you should call the function for each sub array

NoBBy
  • 497
  • 4
  • 15