Megha Paul
Megha Paul

Reputation: 385

Convert three columns from a 2d array into an associative array of associative arrays

I have got an array which is similar to this:

Array
(
[0] => Array
    (
        [cartId] => 51
        [attributeId] => 171
        [Value] => 34
        [quantity] => 1
    )

[1] => Array
    (
        [cartId] => 51
        [attributeId] => 170
        [Value] => Adult
        [quantity] => 1
    )

[2] => Array
    (
        [cartId] => 52
        [attributeId] => 171
        [Value] => 36
        [quantity] => 1
    )

[3] => Array
    (
        [cartId] => 52
        [attributeId] => 170
        [Value] => Adult
        [quantity] => 1
    )
)

I want rearrange it this way:

Array
(
[51] => Array
    (
        [171] => 34
        [170] => Adult
    )

[52] => Array
    (
        [171] => 36
        [170] => Adult
    ) 
)

Basically what i want is the cartId will be parent key and attributeId will be sub-key and value will be its respective value and all records will come under one cartId.

Upvotes: -1

Views: 88

Answers (3)

mickmackusa
mickmackusa

Reputation: 48031

This grouping can be directly achieved using a body-less loop with array destructuring. The new array keys are assigned in advance so that the Value values are pushed into the associative second level of the result array. Demo

$result = [];
foreach (
    $array
    as
    ['cartId' => $c, 'attributeId' => $a, 'Value' => $result[$c][$a]]
);
var_export($result);

For a functional-style approach, use array_reduce() and the union operator to build the associative structure. Demo

var_export(
    array_reduce(
        $array,
        fn($result, $row) => $result
            + [$row['cartId'] => ['attributeId' => $row['Value']]],
        []
    )
);

Upvotes: 1

Nono
Nono

Reputation: 7302

Hmmm.. it's very simple:

PHP Code:

<?php 
$data = array();
$result = array();

$data[] = array('cartId'=>51,'attributeId'=>171,'Value'=>34,'quantity'=>1);
$data[] = array('cartId'=>51,'attributeId'=>170,'Value'=>'Adult','quantity'=>1);
$data[] = array('cartId'=>52,'attributeId'=>171,'Value'=>36,'quantity'=>1);
$data[] = array('cartId'=>52,'attributeId'=>170,'Value'=>'Adult','quantity'=>1);
$data[] = array('cartId'=>53,'attributeId'=>171,'Value'=>45,'quantity'=>1);
$data[] = array('cartId'=>53,'attributeId'=>170,'Value'=>'Adult','quantity'=>1);

foreach($data as $item)
{
   $result[$item['cartId']][$item['attributeId']]=$item['Value'];
}

echo "<pre>";
print_r($result);
echo "</pre>";

Output:

Array
(
    [51] => Array
        (
            [171] => 34
            [170] => Adult
        )

    [52] => Array
        (
            [171] => 36
            [170] => Adult
        )

    [53] => Array
        (
            [171] => 45
            [170] => Adult
        )

)

Upvotes: 1

Alma Do
Alma Do

Reputation: 37365

So that will be:

$result = [];
foreach($array as $item)
{
   $result[$item['cartId']][]=[$item['attributeId']=>$item['value']];
}

Upvotes: 2

Related Questions