簡體   English   中英

如何根據另一列獲取不同的數組值

[英]How to get Distinct array values based on another column

我有這個示例數組 - 從數據庫查詢返回:

array (size=107)
  0 => 
    object(stdClass)[2310]
      public 'country' => string 'Canada' (length=10)
      public 'province' => string 'Ontario' (length=14)
      public 'city' => string 'Toronto' (length=14)
      public 'living_cost' => string 'high' (length=6)
  1 => 
    object(stdClass)[2311]
      public 'country' => string 'Canada' (length=10)
      public 'province' => string 'Ontario' (length=14)
      public 'city' => string 'Barrie' (length=14)
      public 'living_cost' => string 'low' (length=6)
  2 => 
    object(stdClass)[2311]
      public 'country' => string 'Canada' (length=10)
      public 'province' => string 'Nova Scotia' (length=14)
      public 'city' => string 'Halifax' (length=14)
      public 'living_cost' => string 'medium' (length=6)
  3 => 
    object(stdClass)[2312]
      public 'country' => string 'USA' (length=10)
      public 'province' => string 'California' (length=14)
      public 'city' => string 'Los Angeles' (length=14)
      public 'living_cost' => string 'high' (length=6)
  4 => 
    object(stdClass)[2313]
      public 'country' => string 'USA' (length=10)
      public 'province' => string 'Ohio' (length=14)
      public 'city' => string 'Dayton' (length=14)
      public 'living_cost' => string 'low' (length=6)
  5 => 
    object(stdClass)[2314]
      public 'country' => string 'USA' (length=10)
      public 'province' => string 'Illinois' (length=14)
      public 'city' => string 'Chicago' (length=14)
      public 'living_cost' => string 'high' (length=6)
  6 => 
    object(stdClass)[2315]
      public 'country' => string 'USA' (length=10)
      public 'province' => string 'Illinois' (length=14)
      public 'city' => string 'Aurora' (length=14)
      public 'living_cost' => string 'med' (length=6)

我想獲得獨特的國家價值觀,所以:

  • 加拿大
  • 美國

然后對於每個,我想列出省和市,例如:

  • 加拿大
    • 安大略省
      • 多倫多
      • 巴里

美國

  • 加利福尼亞
    • 洛杉磯
  • 伊利諾伊州
    • 芝加哥
    • 極光

我想出了如何獲得獨特的國家,使用:

$countries = array_unique(array_column($db_data, 'country'));

但是現在不知道如何使用國家數組來解析數據庫結果以獲得唯一的省和市。

請記住,我並不是要獲取每個字段的唯一值——我需要先按國家/地區過濾,這樣我仍然可以將省市與國家聯系起來。

換句話說:對於每個國家,我想檢索所有省份和所有城市的列表。

任何幫助是極大的贊賞!

$city1 = new stdClass();
$city1->country = 'Canada';
$city1->province = 'Ontario';
$city1->city = 'Toronto';
$city1->living_cost = 'high';

$city2 = new stdClass();
$city2->country = 'Canada';
$city2->province = 'Ontario';
$city2->city = 'Barrie';
$city2->living_cost = 'low';

$city3 = new stdClass();
$city3->country = 'Canada';
$city3->province = 'Nova Scotia';
$city3->city = 'Halifax';
$city3->living_cost = 'medium';

$city4 = new stdClass();
$city4->country = 'USA';
$city4->province = 'California';
$city4->city = 'Los Angeles';
$city4->living_cost = 'high';

$city5 = new stdClass();
$city5->country = 'USA';
$city5->province = 'Ohio';
$city5->city = 'Dayton';
$city5->living_cost = 'low';

$city6 = new stdClass();
$city6->country = 'USA';
$city6->province = 'Illinois';
$city6->city = 'Chicago';
$city6->living_cost = 'high';

$city7 = new stdClass();
$city7->country = 'USA';
$city7->province = 'Illinois';
$city7->city = 'Aurora';
$city7->living_cost = 'medium';

$data = [ $city1, $city2, $city3, $city4, $city5, $city6, $city7 ];

$result = [];

foreach ($data as $item) {
  $country = $item->country;
  $province = $item->province;
  $city = $item->city;
  $result[$country][$province][$city] = $item;
}


print_r($result);

也許嘗試這樣的東西 $array 應該是數據庫中的數組結果。

` $array = 數組(

0 => [

'city' => 'title 1',
'country' => 'green',
'province' => 'green'

], 1=> [

    'city' => 'title 1',
'country' => 'green',
'province' => 'green'

], 2 => [

     'city' => 'title 1',
'country' => 'green',
'province' => 'green'

]);

$uniqueEntries = 數組();

for($i = 0; $i < sizeof($array); $i++){

$entry = $array[$i];
$country = $entry["country"];
$province = $entry["province"];
$city = $entry["city"];
$key = $country . "" . $province . "" .$city;

$uniqueEntries[$key] = $entry;

}

`

最后迭代唯一條目數組

基於我在這里找到的另一個解決方案,這似乎對我有用:

   $countries = array_unique(array_column($db_data, 'country'));

    foreach ( $countries as $country ) {
        $subset = array_filter($db_data, function ($obj) use ($country) {
            return ($obj->country == $country);
        });

        $cities = array_unique(array_column($subset, 'cities'));

        echo $country. "<br>";
        echo print_r($cities) . '<br><br>';
    }

也許有更好的方法來實現這一點,但它似乎工作。

暫無
暫無

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

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