1 回答
TA贡献1802条经验 获得超10个赞
$comapy = array("ABC","BBC","BBC","ABC");
$price = array(10,5,20,15);
//Group Your data Here ABC[10,15] ..
$group = array();
foreach($comapy as $key=>$val){
$group[$val][] = $price[$key];
}
// this loop for check the max number and count total price
$data = array();
$total = 0;
foreach($group as $key=>$val){
$data[][$key] = $key."-".current($val);
$data[][$key] = $key."-".max($val)." Max price : ".max($val);
$total +=max($val);
}
// this foreach to convert your data to string
$result = "";
foreach($data as $key){
$result .= "\n".current($key);
}
// and show your data like string
print_r($result);
print_r("\nTotal Price ".$total);
/**
result :
ABC-10
ABC-15 Max price : 15
BBC-5
BBC-20 Max price : 20
Total Price 35
//*************If you have more than 4 data in your array remplace foreach
$groups with this code
foreach($group as $key=>$val){
for($i=0;$i<count($val);$i++){
if($val[$i]!==max($val))
$data[][$key] = $key."-".$val[$i];
}
$data[][$key] = $key."-".max($val)." Max price : ".max($val);
$total +=max($val);
}
**/
- 1 回答
- 0 关注
- 119 浏览
添加回答
举报