0

I want to try to merge and sort a multidimensional array. Currently the array looks like this:

Array
(
[0] => Array
    (
        [customerID] => 1234
        [service]    => Car
        [CSA]        => Jack
        [status]     => 3
    )

[1] => Array
    (
        [customerID] => 1234
        [service]    => Cap
        [CSA]        => Jill
        [status]     => 3
    )

[2] => Array
    (
        [customerID] => 1234456
        [service]    => Plate
        [CSA]        => Jack
        [status]     => 1
    )

)

In this multidimensional array, The customerID will be unique, however, many second-level arrays feature the same customerID. Similarly, in these arrays, the CSA could be the same along with the status.

I want the end array to look as follows:

Array
(
[0] => Array
    (
        [customerID] => 1234
        [service]    => Car <br/> Cap
        [CSA]        => Jack <br /> Jill
        [status]     => 3 
    )

[2] => Array
    (
        [customerID] => 1234456
        [service]    => Plate
        [CSA]        => Jack
        [status]     => 1
    )

)

Now, if the service is the same in a set where the customerID is the index, then it shouldn't be added to the value string. The same goes for everything else but the CustomerID.

How do I do this with PHP?

2 Answers 2

1

You can control customerID as array key.

Base example:

$arr = array(/** **/);

$arrayResult = array();

foreach ($arr as $itemResult) {
  if (!isset($arrayResult[$itemResult['customerID']])) {
    $arrayResult[$itemResult['customerID']] = $itemResult;
    continue;
  }

  // Adding service
  $arrayResult[$itemResult['customerID']]['service'] .= '<br />' . $itemResult['service'];
  // Adding CSA
  $arrayResult[$itemResult['customerID']]['CSA'] .= '<br />' . $itemResult['CSA'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this if you don't mind an output array that uses the customer ID as array keys:

$finalArray = array(); // This will be the output array.
foreach ($mainArray as $subArray) { // Iterate through the multidimensional array.
    $currentCustomerID = $subArray['customerID'];
    if (!array_key_exists($currentCustomerID, $finalArray)) { // If the customer hasn't been loaded into the output array yet, load the current array in.
        $finalArray[$currentCustomerID] = $subArray;
    }
    else { // If the customer has already been loaded into the output array, concatenate the service and CSA values.
        $finalArray[$currentCustomerID]['service'] .= ' <br /> '.$subArray['service'];
        $finalArray[$currentCustomerID]['CSA'] .= ' <br /> ' . $subArray['CSA'];
        // Not sure how you want to handle the status, but you can do something like:
        // if ($subArray['status'] > $finalArray[$currentCustomerID]['status']) {
        //     $finalArray[$currentCustomerID]['status'] = $subArray['status'];
        // }
        // ...or using whatever other conditions you need to handle it.
    }

}

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.