繁体   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