I'm relatively new to arrays so bear with me. Situation: a = 1 b = 3 c = 5
1st question: How do I echo the largest value's key: "c" to the page?
2nd questi开发者_如何学Goon: In array_multisort($count, SORT_DESC, $elecCondSort); below, why doesn't it work if I remove $elecCondSort from the parameter?Not sure if my code is the best approach (please if simpler solution exists). Please explain any modifications also.
$elecCondSort = array(
array ("Condition" => "New", "Count" => 5),
array ("Condition" => "Used", "Count" => 3),
array ("Condition" => "Manufacturer refurbished", "Count" => 1),
);
foreach ($elecCondSort as $key => $row)
{
$condition[$key] = $row["Condition"];
$count[$key] = $row["Count"];
}
array_multisort($count, SORT_DESC, $elecCondSort);
echo $condition[$key].'<br>';
if you want the max Count value and the label of the condition, you can:
$elecCondSort = array(
array ("Condition" => "New", "Count" => 5),
array ("Condition" => "Used", "Count" => 3),
array ("Condition" => "Manufacturer refurbished", "Count" => 1),
);
$maxValue = 0;
$labelMaxValue = '';
foreach ($elecCondSort as $item) {
$labelMaxValue = ($item['Count']>$maxValue) ? $item['Condition'] : $labelMaxValue;
$maxValue = ($item['Count']>$maxValue) ? $item['Count'] : $maxValue;
}
echo $labelMaxValue;
echo $maxValue;
i don't really understand how you want to sort the array, but this code should do it for the max value.
精彩评论