如何以阵列中的最低价格获得独特价值

时间:2015-07-13 04:48:20

标签: php arrays multidimensional-array

我在这个阵列中遇到问题可以请任何人帮助我

 Array
(
[0] => Array
    (
        [id] => 41
        [make] => Maruti Suzuki
        [model] => Alto 800
        [variant] => LXI
        [variantid] => 2628
        [city] => Mumbai
        [car_price] => 321279
        [img] => maruti-suzuki-alto-800-default-transparent.png
    )


[3] => Array
    (
        [id] => 38
        [make] => Maruti Suzuki
        [model] => Alto 800
        [variant] => Lx
        [variantid] => 2627
        [city] => Mumbai
        [car_price] => 298655
        [img] => maruti-suzuki-alto-800-default-transparent.png
    )

 )

这里我的问题是我需要获得唯一一个价格最低的中音800,请建议我任何一个

提前致谢

5 个答案:

答案 0 :(得分:2)

您可以使用array_multisort(PHP 4,PHP 5)和array_map(PHP 4> = 4.0.6,PHP 5)

// Sort your array in ascending order
array_multisort(array_map(function($_){return $_["car_price"];},$array), SORT_ASC, $array);

// To get array of min car price
print_r($array[0]);

// To get only car price
echo $array[0]["car_price"];

如果您有 PHP 5> = 5.5.0 ,请使用array_column

array_multisort(array_column($array,"car_price"), SORT_ASC, $array);

<强>测试

[akshay@localhost tmp]$ cat test.php 
<?php

$array = array( 
              array(
                   "id"=>41,
                   "make"=>"Maruti Suzuki",
                   "model"=>"Alto 800",
                   "variant"=>"LXI",
                   "variantid"=>2628,
                   "city"=>"Mumbai",
                   "car_price"=>321279,
                   "img"=>"maruti-suzuki-alto-800-default-transparent.png"
                  ),
             array(
                   "id"=>38,
                   "make"=>"Maruti Suzuki",
                   "model"=>"Alto 800",
                   "variant"=>"Lx",
                   "variantid"=>2627,
                   "city"=>"Mumbai",
                   "car_price"=>298655,
                   "img"=>"maruti-suzuki-alto-800-default-transparent.png"
                  ),
              );

array_multisort(array_map(function($_){return $_["car_price"];},$array), SORT_ASC, $array);

// To get array of min car price
print_r($array[0]);

// To get only car price
echo $array[0]["car_price"];

?>

<强>输出

[akshay@localhost tmp]$ php test.php 
Array
(
    [id] => 38
    [make] => Maruti Suzuki
    [model] => Alto 800
    [variant] => Lx
    [variantid] => 2627
    [city] => Mumbai
    [car_price] => 298655
    [img] => maruti-suzuki-alto-800-default-transparent.png
)
298655

答案 1 :(得分:0)

这样的事情应该有效,但请注意它没有优化:

<?php
$min_price=9999999;
$best_deal=array();
foreach($array as $car){
    if($car['model']=="Alto 800" && $car['price']<$min_price){
        $min_price=$car['price'];
        $best_deal=$car;
    }
}

?>

最便宜的汽车将在变量$best_deal中,如果它不是空的array(在这种情况下,它意味着所有价格都高于 9999999 或您的{{ 1}} 为空

答案 2 :(得分:0)

您可以使用辅助数组并使用min函数

<?php
$tmp = []; // or $tmp = array() depending on how recent your PHP version is
foreach($cars as $car) {
    $tmp[] = $car['car_price'];
}
$lowest = min($tmp);
foreach($cars as $car) {
    if($car['car_price'] == $lowest) {
        // Your car is now $car
    }
}
?>

请注意,如果您的汽车价格相同且超过1辆,则会选择阵列中的第一辆汽车。

答案 3 :(得分:0)

如果您使用的是PHP 5.3,请尝试使用usort

usort($myArray, function($a, $b) {
    return $a['car_price'] - $b['car_price'];
});
echo "<pre>";
print_r($myArray[0] );

答案 4 :(得分:-1)

我会让你进入一个小秘密...

SELECT id, make, model, variant, variantid, city, car_price, img
    FROM sometable
    ORDER BY car_price ASC;

现在你根本不用PHP排序:)