将多维数组中的唯一值分组

时间:2019-06-30 08:10:29

标签: php arrays multidimensional-array

我有一个RestroreModels数组

如果RestoreCode值在数组中多次存在。基于唯一的还原,我想添加TotalCharge值,并将具有该RestoreCode的关联模型放入新数组中。如果RestoreCode值仅按数组中单个模型的原样存在一次。

$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+',
    )
  );


我想要这样的结果

$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'), 
    ),
  );

到目前为止,我已经尝试过此操作,但是我能够获得唯一的值,但是不确定如何添加addTotalCharge并将与RestoreCode相关的模型放入$ 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();

1 个答案:

答案 0 :(得分:1)

考虑到笔记if the RestoreCode value exist only once put as it is with single model in array,我认为您可以使用foreach循环并将单个数组的RestoreCode存储为结果数组中的键。请注意,该结果将与所需结果不同,因为带有单个RestoreCode的项目也将出现在结果中。

每次迭代检查该键是否已经存在。如果不是,则添加它,然后将Model的值放在新数组中。

如果确实存在,请添加TotalCharge的值。要获取模型的唯一值,请检查Model的当前数组是否已包含它。

如果您想重置密钥,可以使用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