SirOracle
SirOracle

Reputation: 393

Transpose an associative array of indexed arrays

Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure how to explain it exactly so hopefully these print_r results explain it better.

Current Array

Array
(
    [qty] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
        )

    [item_id] => Array
        (
            [0] => RR332
            [1] => FF586
            [2] => QW865
        )

    [item_name] => Array
        (
            [0] => Widget 1
            [1] => Widget 2
            [2] => Widget 3
        )

    [price] => Array
        (
            [0] => 1.00
            [1] => 1.50
            [2] => 1.50
        )

)

Wanted Array

Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

This seems like it should be a simple task but I haven't been able to crack it yet.

I would preferably like to loop through the Current Array to create the Wanted Array if possible.

Upvotes: 2

Views: 139

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270637

I think this will do the job for you, though I don't have a good data set to test against.

$wanted_array = array();
// Iterate over the outer array to get the text keys & inner arrays
foreach ($current_array as $txtkey=>$arr) {
  // Iterate over each inner array to get the numeric key
  foreach ($arr as $numkey=>$val) {
    // Set a numeric key pointing to a new array 
    // onto the new outer array if it isn't already there
    if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();

    // Swap the keys of the outer and inner arrays.
    $wanted_array[$numkey][$txtkey] = $val;
  }
}

Update: Using test array from @hakre's answer, here it is in practice:

$current_array = array(
  'qty' => array(
    0 => 1,
    1 => 1,
    2 => 1
  ),
  'item_id' => array(
    0 => 'RR332',
    1 => 'FF586',
    2 => 'QW865'
  ),
  'item_name' => array(
    0 => 'Widget 1',
    1 => 'Widget 2',
    2 => 'Widget 3'
  ),
  'price' => array(
    0 => '1.00',
    1 => '1.50',
    2 => '1.50'
  )
);
// Output:
Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

Upvotes: 3

hakre
hakre

Reputation: 197777

You mean this?:

$data = array(
  'qty' => array(
    0 => 1,
    1 => 1,
    2 => 1,
  ),
  'item_id' => array(
    0 => 'RR332',
    1 => 'FF586',
    2 => 'QW865',
  ),
  'item_name' => array(
    0 => 'Widget 1',
    1 => 'Widget 2',
    2 => 'Widget 3',
  ),
  'price' => array(
    0 => '1.00',
    1 => '1.50',
    2 => '1.50',
  ),
);

$result = array();
foreach($data as $key => $values)
{
    foreach($values as $number => $value)
        $result[$number][$key] = $value;
}

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

Upvotes: 4

Jessedc
Jessedc

Reputation: 12460

You can loop over your original array using the foreach loop, which gives you the $key, and $value for each item in your array. From there, construct your new array as you like.

$array2 = array();
foreach ($array1 as $key => $value)
{
  $array2[] = array('key' => 'value');
}

Upvotes: 0

Alex Turpin
Alex Turpin

Reputation: 47776

Like you said, just loop through it to build it.

$wanted = array();
foreach($current['item_id'] as $key => $value) {
    $wanted[$key] = array(
        'qty' = $current['qty'][$key],
        'item_id' = $current['item_id'][$key],
        'item_name' = $current['item_name'][$key],
        'price' = $current['price'][$key]
    );
}

print_r($wanted);

Upvotes: 1

Related Questions