php多维数组 - 如何查找最小值和返回键

时间:2017-06-23 04:37:19

标签: php arrays multidimensional-array

我有一个独特的问题,我似乎无法找到答案。

我最终想要找到具有最小值的数组的索引'价格'。

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

您可能还会注意到价格'重复值为10.在这种情况下,是否可以比较$r[0]['day']$r[3]['day']值,然后提出$index=0?如果PRICE和DAY相同,那么任何一个都没问题。

我喜欢的结果是print "Free Shipping".$r[$index]['name']." ".$r[$index]['day']." Business Days".

修改 我发现我的数组不同,没有索引,但现在有名称参考。

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

最后编辑谢谢大家的帮助!我已经将代码@Kolob Canyon和@sathish R结合起来以获得我的最终代码。

$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";
    }

5 个答案:

答案 0 :(得分:1)

感觉像是在解决你的家庭作业问题:

$min = PHP_INT_MAX;
$idx = null;
for($i=0; $i < count($r); $i++)
{
       if($min > $r[$i]['price']) 
       { 
            $min = $r[$i]['price'];
            $idx = $i;
       }
}

if($idx != null) {
    echo "Free Shipping". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days"; 
}

回答您的修改。

<?php
$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)
);

$min = PHP_INT_MAX;
$idx = null;
foreach ($r as $key => $value) {
        if($min > $r[$key]['price'])
        {
            $min = $r[$key]['price'];
            $idx = $key;
        }
    }

    if($idx != null) {
        echo "Free Shipping ". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
    }
?>

答案 1 :(得分:1)

你可以使用简单的for循环来实现你的结果。

$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)); $minPrice=PHP_INT_MAX ,$minDay =PHP_INT_MAX,$index=-1 ; $i=0; foreach($r as $d) { if($d['price'] < $minPrice) { $minPrice = $d['price']; $minDay = $d['day']; $index = $i; } else if($d['price'] == $minPrice ) { if($d['day'] < $minDay ) { $minDay = $d['day']; $index = $i; } } $i++; } if($index >=0 ) { echo "Free Shipping". $r[$index]['name'] . " " . $r[$index]['day'] . " Business Days"; }

答案 2 :(得分:0)

你可以这样做:

$dealersMin = min(array_column($dealers, 'count'));

$dealersWithMinCount = array_filter($dealers, function ($dealer) {
    global $dealersMin;
    return ($dealer['count'] == $dealersMin);
});

的var_dump($ dealersWithMinCount [array_rand($ dealersWithMinCount)] [&#39; ID&#39;]);

希望这会奏效......

您可以在此处查看更多内容https://eval.in/819623

答案 3 :(得分:0)

使用本机函数asort重新排序数组

<?php

// your array
$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)
);

// getting the columns
$col_id     = array_column($r, 'id');   
$col_name   = array_column($r, 'name');
$col_day    = array_column($r, 'day');
$col_price  = array_column($r, 'price');


// reordering the columns - first the price, second the day
$new_r = array();

$i=0;
foreach($col_id as $id)
{
    $new_r[] = array(
                    'price'=>$col_price[$i],
                    'day'=>$col_day[$i],
                    'id'=>$id,
                    'name'=>$col_name[$i],
                    );
    $i++;
}

// asort the new array - php will do what you want following the order of the columns       
asort($new_r);      
var_dump($new_r);   

//Then get the desired element (the first in the array above)

$free = array_shift($new_r); //get first element of the array $new_r
print "Free Shipping: ".$free['name']." ".$free['day']." Business Days";

脚本输出:

array (size=4)
  0 => 
    array (size=4)
      'price' => int 10
      'day' => int 1
      'id' => int 1526
      'name' => string 'Expedited Parcel' (length=16)
  3 => 
    array (size=4)
      'price' => int 10
      'day' => int 15
      'id' => int 1332
      'name' => string 'Snail Mail Shipping' (length=19)
  1 => 
    array (size=4)
      'price' => int 20
      'day' => int 2
      'id' => int 1234
      'name' => string 'XpressPost Parcel' (length=17)
  2 => 
    array (size=4)
      'price' => int 100
      'day' => int 7
      'id' => int 5345
      'name' => string 'Internation Shipping' (length=20)

Free Shipping: Expedited Parcel 1 Business Days

让测试更改价格和日期的值。

答案 4 :(得分:0)

代码:

<?php
$temp_array = array();
$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));

foreach($r as $row)
{
    if(count($temp_array)!=0)
    {
        if($temp_array['price']>$row['price'])
        {
            $temp_array['id'] = $row['id'];
            $temp_array['name'] = $row['name'];
            $temp_array['day'] = $row['day'];
            $temp_array['price'] = $row['price'];
        }
        else if($temp_array['price'] == $row['price'])
        {
            if($temp_array['day']>$row['day'])
            {
                $temp_array['id'] = $row['id'];
                $temp_array['name'] = $row['name'];
                $temp_array['day'] = $row['day'];
                $temp_array['price'] = $row['price'];
            }
        }
    }
    else
    {
        $temp_array['id'] = $row['id'];
        $temp_array['name'] = $row['name'];
        $temp_array['day'] = $row['day'];
        $temp_array['price'] = $row['price'];
    }
}
echo "Free Shipping :".$temp_array['name'].", ".$temp_array['day']." Business Days";
?>

输出:

Free Shipping :Expedited Parcel, 1 Business Days