简体   繁体   中英

How to get Distinct array values based on another column

I have this sample array - as returned from a database query:

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)

I'd like to get the unique Country values, so:

  • Canada
  • USA

And then for each, I'd like to list the province and cities, for example:

  • Canada
    • Ontario
      • Toronto
      • Barrie

USA

  • California
    • Los Angeles
  • Illinois
    • Chicago
    • Aurora

I figured out how to get the unique countries, using:

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

But not sure how to use the country array to now parse the database result to get the unique province and cities.

Please keep in mind, I'm not trying to get the unique values of each field - I need to first filter by the country so I can still relate the provinces and cities back to the country.

In other words: for each country, I want to retrieve a list of all provinces and all cities.

Any help is greatly appreciated!

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

Maybe try somthing like this The $array should be your array result from database.

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

}

`

Finally iterate the unique entries array

Based on another solution I found here, this seems to work for me:

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

Maybe there is a better way to achieve this, but it seems to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM