簡體   English   中英

從子多維php獲取最大值和最小值

[英]get max and min value from child multidimensional php

我有這樣的數組:

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

您應該初始化數組以將價格和評分值放在此處。 同樣,當您添加另一個值時,您應該將其放入數組的另一個元素。 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); 

如果我沒有誤解您的問題,那么您必須嘗試這樣分配每個價格和等級,例如$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);

$ rest = $ listRest ['restaurants'];

foreach($ listRest作為$ 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)將不起作用。
  • 同樣,由於$ price和$ rating是在forloop內部定義的,因此無法在其外部訪問。
    • 因此,在$ 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'];

您只需要將所有數據放入一個數組,然后在其上執行max()即可。 我創建了一個實時PHP腳本來更正您的代碼: 從子多維php獲取最大值和最小值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM