从子多维php获取最大值和最小值

时间:2017-10-17 09:59:14

标签: php arrays multidimensional-array

我有这样的数组:

$listRest = 
Array
([restaurants] => Array
        (
            [0] => Array
                (
                    [restaurant] => Array
                        (
                            [name] => Pipe and Puff
                            [location] => Array
                                (
                                    [city_id] => 74
                                )
                            [average_cost_for_two] => 150000
                            [user_rating] => Array
                                (
                                    [aggregate_rating] => 3.8
                                )
                        )
                )

        [1] => Array
            (
                [restaurant] => Array
                    (
                        [location] => Array
                        (
                                [city_id] => 74
                            )
                        [average_cost_for_two] => 300000
                        [user_rating] => Array
                            (
                                [aggregate_rating] => 3.4
                            )
                    )
            )

我想找到“average_cost_for_two”和“aggregate_rating”的最小值,但是它说“max():当只给出一个参数时,它必须是一个数组”

这是我的代码

$rest = $listRest['restaurants'];
foreach($listRest as $value)
{
  $price = $value['restaurant']['average_cost_for_two'];
  $rating = $value['restaurant']['user_rating']['aggregate_rating'];
}
echo max($price)."</br>.";
echo max($rating);

4 个答案:

答案 0 :(得分:3)

您应该初始化数组以将价格和评级值放在那里。此外,当您添加另一个值时,您应该放入另一个数组元素。 Max函数从数组中选择最高值。

$rest = $listRest['restaurants'];
$price = [];
$rating = [];
foreach($rest as $value)
{
  $price[] = $value['restaurant']['average_cost_for_two'];
  $rating[] = $value['restaurant']['user_rating']['aggregate_rating'];
}
echo max($price)."</br>.";
echo max($rating); 

答案 1 :(得分:1)

如果我没有理解你的问题,你可以尝试分别分配这个$price[]$rating[]的每个价格和评级。

$price = $rating = [];
$rest = $listRest['restaurants'];
foreach($listRest as $value)
{
  $price[] = $value['restaurant']['average_cost_for_two'];   // see the change here
  $rating[] = $value['restaurant']['user_rating']['aggregate_rating']; // see the change here
}
echo max($price)."</br>.";
echo max($rating);

echo min($price)."</br>.";
echo min($rating);

答案 2 :(得分:1)

  

$ rest = $ listRest ['restaurants'];

     

foreach($ listRest as $ value)

应该是

$rest = $listRest['restaurants']; 

foreach($rest as $value)
  

$ price = $ value ['restaurant'] ['average_cost_for_two'];

     

$ rating = $ value ['restaurant'] ['user_rating'] ['aggregate_rating'];

  • 将覆盖以前的$ price和$ rating值,因此,max($ price)和max($ rating)将无效。
  • 此外,在forloop中定义了$ price和$ rating,它们将无法在其外部访问。
    • 因此,请将forloop之外的$ price和$ rating实例化为$price[]$rating[]
  

echo max($ price)。“。”;

     

echo max($ rating);

Max函数适用于数组,因此在forloop中你必须将循环获得的每个值推送(插入)分别为$ price和$ rating,

$price[] = $value['restaurant']['average_cost_for_two'];
$rating[] = $value['restaurant']['user_rating']['aggregate_rating'];

答案 3 :(得分:0)

你只需要将所有数据放在一个数组中,然后在其上执行max()。 我创建了一个实时PHP脚本来纠正您的代码:get max and min value from child multidimensional php