2

I have a multidimensional array that looks like such:

 Array
(
[0] => Array
    (
        [email] => [email protected]
        [added] => style-narcotics
    )

[1] => Array
    (
        [email] => [email protected]
        [added] => style-edm
    )

[2] => Array
    (
        [email] => [email protected]
        [added] => style-codeine
    )

[3] => Array
    (
        [email] => [email protected]
        [added] => style-food
    )
  )

I want to merge all the inner arrays combining the "added" key like such:

Array
(
[0] => Array
    (
        [email] => [email protected]
        [added] => array(
                        [0]=>style-narcotics
                        [1]=>style-edm
                        )
    )



[1] => Array
    (
        [email] => [email protected]
        [added] => array(
                        [0]=>style-codeine
                        [1]=>style-food
    )

  )

I have tried merge array recursive in different forms and call_user_func but it doesnt cut it. Any advice? Thanks!

2
  • You want to change the type of added from string to array (add another nesting level) but array_merge_recursive() doesn't do that. It doesn't change the structure of the arrays it receives as arguments. It just combines them. You have to iterate over the input array and create the desired output. Commented Apr 15, 2016 at 19:57
  • good idea. that makes more sense. I fixed that part. I may be able to figure something out. I will post an update if I figure it out. Thanks @axiac Commented Apr 15, 2016 at 20:10

2 Answers 2

3

I would call it "grouping", but not "merging".
Use the following approach with array_walk and array_values functions:

$grouped = [];
// $arr is your initial array
array_walk($arr, function($v) use (&$grouped){
    if (array_key_exists($v["email"], $grouped)) {
        $grouped[$v["email"]]["added"][] = $v["added"];
    } else {
        $v["added"] = [$v["added"]];
        $grouped[$v["email"]] = $v;
    }
});

print_r(array_values($grouped));

The output:

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [added] => Array
                (
                    [0] => style-narcotics
                    [1] => style-edm
                )

        )

    [1] => Array
        (
            [email] => [email protected]
            [added] => Array
                (
                    [0] => style-codeine
                    [1] => style-food
                )
        )
)
Sign up to request clarification or add additional context in comments.

Comments

1

A simple solution that uses array_reduce():

$output = array_reduce(
    $input,
    function (array $carry, array $item) {
        $email = $item['email'];
        if (! isset($carry[$email])) {
            // It's a new email, make room for it in the output
            $carry[$email] = array('email' => $email, 'added' => array(), );
        }
        // Add the value of $item['added'] into the existing array
        $carry[$email]['added'][] = $item['added'];
        return $carry;
    },
    array()
);

// Output is indexed by email addresses. If you need numeric keys then...
$output = array_values($output);

The same logic as above but with an explicit iteration over the input array (the code is a couple of lines shorter):

$output = array();
foreach ($input as $item) {
    $email = $item['email'];
    if (! isset($carry[$email])) {
        // It's a new email, make room for it in the output
        $carry[$email] = array('email' => $email, 'added' => array(), );
    }
    // Add the value of $item['added'] into the existing array
    $carry[$email]['added'][] = $item['added'];
}
$output = array_values($output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.