-4

我有一个包含城市的数组,我想返回所有这些城市的数组,但它必须是城市的唯一列表。

下面的数组:

Array
(
    [0] => Array
        (
            [eventname] => Wine Tasting
            [date] => 12/20/2013
            [time] => 17:00:00
            [location] => Anaheim Convention Center
            [description] => This is a test description
            [city] => Anaheim
            [state] => California
        )

    [1] => Array
        (
            [eventname] => Circus
            [date] => 12/22/2013
            [time] => 18:30:00
            [location] => LAX
            [description] => Description for LAX event
            [city] => Anaheim
            [state] => California
        )

    [2] => Array
        (
            [eventname] => Blues Fest
            [date] => 3/14/2014
            [time] => 17:00:00
            [location] => Austin Times Center
            [description] => Blues concert
            [city] => Austin
            [state] => Texas
        )

)

应该返回:

array('Anaheim', 'Austin');

任何帮助表示赞赏。

4

3 回答 3

2

用于array_map获取所有城市,然后array_unique删除重复项。

$cities = array_unique(array_map(function($x) { return $x['city']; }, $array));
于 2013-11-11T21:52:37.603 回答
1

您可以将它们添加为强制它唯一的键,然后翻转它,或者:

foreach($events as $details) {
    $cities[] = $details['city'];
}
$cities = array_unique($cities);
于 2013-11-11T21:50:18.973 回答
0
foreach ($array as $val) {
  $cities[$val['city']] = '';
}
$cities = array_keys($cities);
于 2013-11-11T21:51:12.493 回答