I have a unique problem that I cannot seem to find an answer to.
I ultimately want to find the index of the array that has the min value on 'price'.
$r = array (
array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10));
You might also notice that the 'price' has duplicate values of 10. In this case, would it be possible to then compare the $r[0]['day']
and $r[3]['day']
values and then come up with $index=0
? And if PRICE and DAY are the same, then either one is fine.
The result I'd like would be print "Free Shipping".$r[$index]['name']." ".$r[$index]['day']." Business Days".
EDIT I found out my array is different, there is no index but now a name reference.
$r = array (
'DOM.EP' => array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
'DOM.PC' => array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
'DOM.IS' => array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
'DOM.SM' => array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10)
);
LAST EDIT Thank you everyone for your help! I've gone ahead and combined code @Kolob Canyon and also @sathish R to get my final code.
$min = 9999999999;
$minDay = 9999999999;
$idx = null;
foreach ($r as $key => $value) {
if($min > $r[$key]['price']) {
$min = $r[$key]['price'];
$minDay = $r[$key]['day'];
$idx = $key;
}
else if ($min == $r[$key]['price']) {
if ($minDay > $r[$key]['day']){
$minDay = $r[$key]['day'];
$idx = $key;
}
}
}
if($idx != null) {
echo "Free Shipping ". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
}