【问题标题】:How to get Distinct array values based on another column如何根据另一列获取不同的数组值
【发布时间】:2022-01-30 19:17:59
【问题描述】:

我有这个示例数组 - 从数据库查询返回:

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)

我想获得独特的 Country 值,所以:

  • 加拿大
  • 美国

然后对于每个,我想列出省份和城市,例如:

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

美国

  • 加利福尼亚
    • 洛杉矶
  • 伊利诺伊州
    • 芝加哥
    • 极光

我想出了如何获得独特的国家,使用:

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

但现在不知道如何使用国家数组来解析数据库结果以获取唯一的省和市。

请记住,我并不是要获取每个字段的唯一值 - 我需要先按国家/地区进行过滤,以便我仍然可以将省市与国家/地区联系起来。

换句话说:对于每个国家,我想检索所有省份和所有城市的列表。

非常感谢任何帮助!

【问题讨论】:

  • 我认为提供的答案没有理解手头的问题。基本上,我需要回答以下问题:“加拿大有哪些独特的省份”,或者“美国有哪些独特的城市”。意思是,如果提供国家/地区,我该如何解析原始数据以检索该国家/地区的唯一城市或省份。

标签: php arrays


【解决方案1】:
$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);

【讨论】:

    【解决方案2】:

    基于another solution I found here,这似乎对我有用:

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

    也许有更好的方法来实现这一点,但它似乎有效。

    【讨论】:

      【解决方案3】:

      也许可以试试这样 $array 应该是数据库中的数组结果。

      $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 = array();
      
      
      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;
      
      }           
      

      最后迭代唯一条目数组

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-18
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        相关资源
        最近更新 更多