php数组和max我有过数据表,需要数组按值分组,选择max值和max值我有想法,但我不能得到正确的值。 数组如下所示,html代码位于底部
Array
(
[company] => Array
(
[0] => ABC
[1] => BBC
[2] => BBC
[3] => ABC
)
[price] => Array
(
[0] => 10
[1] => 5
[2] => 20
[3] => 15
)
[submit] => Submit
)
输出
1-ABC-10
4-ABC-15 …….Max price 15
2-BBC-5
3-BBC-20 ……. Max price 20
Total Price 35
Html表单
<form action="?" method="post" name="form1" id="form1">
Company: <input name="company[]" type="text" id="company[]" value="ABC"><br>
Price: <input name="price[]" type="text" id="price[]" value="10">
<br>
Company: <input name="company[]" type="text" id="company[]" value="BBC"><br>
Price: <input name="price[]" type="text" id="price[]" value="5">
<br>
Company: <input name="company[]" type="text" id="company[]" value="BBC"><br>
Price: <input name="price[]" type="text" id="price[]" value="20">
<br>
Company: <input name="company[]" type="text" id="company[]" value="ABC"><br>
Price: <input name="price[]" type="text" id="price[]" value="15">
<br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
$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
**/