1

I have an array with same customerid. I want to merge all same customerid arrays in to one with few amends to the array.

Array
(
    [0] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [profession_id] => 8
            [profession_name] => Producer
         )

    [1] => Array
        (
            [customerid] => 1
            [customer_fullname] => John
            [profession_id] => 8
            [profession_name] => Producer

        )

    [2] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [profession_id] => 7
            [profession_name] => Camera

        )

)

So now I want a new array to be created like this:

Array(
    [customerid] => 13
    [customer_fullname] => Chris
    [new_array] => array(
        [0]=>[profession_id] => 8, [profession_name] => Producer, 
        [1]=>[profession_id] => 7, [profession_name] => Camera
    )
)

Spent some time on it but wasn't able to get it right

4
  • Do you want to build array for the 1st customerid omly or for all? Commented Jun 4, 2016 at 10:36
  • i am combining all those arrays who have same customerid, so in our case there are two customerid with the same id so we are combining it. Commented Jun 4, 2016 at 10:51
  • And what is about [customerid] => 1 ? Commented Jun 4, 2016 at 10:52
  • we are disregarding that as it doesn't have any matching customerid Commented Jun 4, 2016 at 10:58

3 Answers 3

2

There are better approaches if you're merging lots of records, but if you want a way to just merge two records as stated, I'd just do this:

$array1 = array(
      'customerid' => 13
      'customer_fullname' => 'John',
      'profession_id' => 8,
      'profession_name' => 'Producer'
);
$array2 = array(
      'customerid' => 13
      'customer_fullname' => 'John',
      'profession_id' => 7,
      'profession_name' => 'Director'
);

function merge_customers($customerA, $customerB)
{
    $newCustomer = array();
    if ($customerA['customerid'] == $customerB['customerid'])
    {
        $newCustomer['customerid'] = $customerA['customerid'];
        $newCustomer['customer_fullname'] = $customerA['customer_fullname'];

        $newCustomer['new_array'] = array(
            array(
                'profession_id' => $customerA['profession_id'],
                'profession_name' => $customerA['profession_name']
            ),
            array(
                'profession_id' => $customerB['profession_id'],
                'profession_name' => $customerB['profession_name']
            )
        );

        return $newCustomer;
    }

    /* We can't merge these if they're different customers. */
    return NULL;
}
Sign up to request clarification or add additional context in comments.

3 Comments

actually yes...there could be multiple arrays and not just 2. what could be a better approach?
I'll add that in a few minutes, sure.
Given your update, I would do it the way that @RomanPerekhrest suggested :)
2

The extended solution which is also well-suited for finding and "merging" multiple groups of entries which has same customerid. Used functions: array_filter, array_count_values, array_keys, array_walk, array_chunk and array_values:

// supposing $arr is your initial array

// finds which 'customerid' has multiple entries
$dupIds = array_filter(array_count_values(array_column($arr, "customerid")), function($v) {
    return $v > 1;
});

$dupIds = array_keys($dupIds);
$result = [];

array_walk($arr, function($v) use(&$result, $dupIds) {
    if (in_array($v['customerid'], $dupIds)) {
        $parts = array_chunk($v, 2, true);
        if (!isset($result[$v['customerid']])) {
            $result[$v['customerid']] = $parts[0] + ['new_array' => [$parts[1]]];
        } else {
            $result[$v['customerid']]['new_array'][] = $parts[1];
        }
    }
});

print_r(array_values($result));

The output:

Array
(
    [0] => Array
        (
            [customerid] => 13
            [customer_fullname] => Chris
            [new_array] => Array
                (
                    [0] => Array
                        (
                            [profession_id] => 8
                            [profession_name] => Producer
                        )

                    [1] => Array
                        (
                            [profession_id] => 7
                            [profession_name] => Camera
                        )
                )
        )
)

Comments

1

Quick hack, maybe there is a nicer solution. Note: The second "for each" loop is only needed if there is the possibility that the arrays don't have the same fields.

function merge($array1, $array2){
     $result = array();
     foreach($array1 as $key => $value){
         if(isset($array2[$key]) && $array2[$key]!=$array1[$key]){
              $result[$key][]=$value;
              $result[$key][]=$array2[$key];
         }else{
              $result[$key]=$value;
         }
     }
     foreach($array2 as $key => $value){
        if(!isset($result[$key])){
             $result[$key] = $value;
        }
     }
     return $result;
}

print_r(merge($array1, $array2));

1 Comment

yes...but there could be n number of same customerid's

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.