abid
abid

Reputation: 25

group unique values in multidimensional array

I have an array of RestroreModels

if the RestoreCode value exists multiple times in array. Based on unique restore I want to add the TotalCharge value and put the associated model with that RestoreCode in a new array. if the RestoreCode value exist only once put as it is with single model in array.

$RestroreModels =  array (
    0 => 
    array (
      'TotalCharge' => '3',
      'RestoreCode' => 'FF 0',
      'Model' => 'iPhone 7',
    ),
    1 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'LT 2015',
      'Model' => 'iPad Mini 2',
    ),
    2 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'LT 4013',
      'Model' => 'iPhone 6',
    ),
    3 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'LT 4013',
      'Model' => 'ipod touch',
    ),
    4 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'P 21',
      'Model' => 'iPhone 7',
    ),
    5 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'AL 2015',
      'Model' => 'iPhone 7',
    ),
    6 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'AL 0',
      'Model' => 'ipod touch',
    ),
    7 => 
    array (
      'TotalCharge' => '1',
      'RestoreCode' => 'LT 2015',
      'Model' => 'iPad Mini',
    ),
    8 => 
    array (
      'TotalCharge' => '1',
      'RestoreCode' => 'LT 4005',
      'Model' => 'iPad Mini',
    ),
    9 => 
    array (
      'TotalCharge' => '1',
      'RestoreCode' => 'P 21',
      'Model' => 'iPad 5',
    ),
    10 => 
     array (
      'TotalCharge' => '1',
      'RestoreCode' => 'LT 4013',
      'Model' => 'iPhone 7+',
    )
  );


i want the result like this

$RestroreModels  =  array (

    0 => 
     array (
      'TotalCharge' => '4',
      'RestoreCode' => 'LT 2015',
      'Model' => array ('iPad Mini 2', 'iPhone 7')
    ),
    1 => 
     array (
      'TotalCharge' => '3',
      'RestoreCode' => 'LT 4013',
      'Model' => array ('iPhone 7+', 'ipod touch', 'iPad Mini 2')
    ),
    2 => 
    array (
      'TotalCharge' => '3',
      'RestoreCode' => 'P 21',
      'Model' => array ('iPad Mini 2', 'iPhone 7', 'iPad 5')
    ),
    3 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'AL 2015',
      'Model' => array ('iPhone 7'), 
    ),
    4 => 
    array (
      'TotalCharge' => '2',
      'RestoreCode' => 'AL 0',
      'Model' => array ('ipod touch'), 
    ),
    5 => 
    array (
      'TotalCharge' => '1',
      'RestoreCode' => 'LT 4005',
      'Model' => array ('iPad Mini'), 
    ),
  );

I have Tried this so far i am able to get unique values but not sure how to add add TotalCharge and put model related to RestoreCode in $final_array.

$final_array = array();
$keys_array = array();

foreach ($RestroreModels as $key => $value) {
    if (!in_array($value['RestoreCode'], $keys_array)) {
        $keys_array[$key]       =  $value['RestoreCode'];
        $final_array[$key]      =  $value;
    }

}



var_dump ($final_array);
die();

Upvotes: 1

Views: 82

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

Taking the note if the RestoreCode value exist only once put as it is with single model in array into account, I think you might use a foreach loop and store the RestoreCode of a single array as the key in the result array. Note that the result will differ from your wanted result as the item with a single RestoreCode will also be in the result.

Per iteration check if that key already exists. If if does not, add it and put the value for the Model in a new array.

If it does exist, add the values for TotalCharge. To get the unique values for the Models, check if the current array for Model already contains it.

If you want to reset the keys, you could use array_values.

$res = [];

foreach ($RestroreModels as $item) {
    if (isset($res[$item["RestoreCode"]])) {
        if (!in_array($item["Model"], $res[$item["RestoreCode"]]["Model"], true)) {
            $res[$item["RestoreCode"]]["Model"][] = $item["Model"];
        }
        $res[$item["RestoreCode"]]["TotalCharge"] += $item["TotalCharge"];

    } else {
        $item["Model"] = [$item["Model"]];
        $res[$item["RestoreCode"]] = $item;
    }
}

print_r(array_values($res));

Php demo

Upvotes: 1

Related Questions