Php 从数组返回唯一值
我有一个包含城市的数组,我想返回所有这些城市的数组,但它必须是城市的唯一列表 下面的数组:Php 从数组返回唯一值,php,arrays,Php,Arrays,我有一个包含城市的数组,我想返回所有这些城市的数组,但它必须是城市的唯一列表 下面的数组: Array ( [0] => Array ( [eventname] => Wine Tasting [date] => 12/20/2013 [time] => 17:00:00 [location] => Anaheim Convention Cente
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');
非常感谢您的帮助。您可以将它们添加为一个键,强制其唯一,然后将其翻转,或者:
foreach($events as $details) {
$cities[] = $details['city'];
}
$cities = array_unique($cities);
使用
array\u map
获取所有城市,然后使用array\u unique
删除重复项
$cities = array_unique(array_map(function($x) { return $x['city']; }, $array));
首先遍历上面数组中的所有条目,并将城市选入临时数组,然后在该临时数组上使用phps
array\u unique
函数。“感谢您的帮助”-也感谢您自己的努力。我删除了我的答案。祝你好运:)
$cities = array_unique(array_map(function($x) { return $x['city']; }, $array));