Php 从更多数组中选择唯一值
我有以下代码:Php 从更多数组中选择唯一值,php,mysql,sql,Php,Mysql,Sql,我有以下代码: $get_countries = "SELECT country FROM blahs WHERE country IS NOT NULL ORDER BY country"; $data_get_countries = mysqli_query($dbc, $get_countries); while ($row_get_countries = mysqli_fetch_array($data_get_countries)) { $country_tabs = arr
$get_countries = "SELECT country FROM blahs WHERE country IS NOT NULL ORDER BY country";
$data_get_countries = mysqli_query($dbc, $get_countries);
while ($row_get_countries = mysqli_fetch_array($data_get_countries)) {
$country_tabs = array_unique($row_get_countries);
foreach ($country_tabs as $country_tab) {
echo '<div>' . $country_tab . '</div>';
var_dump($country_tab);
var_dump($country_tabs);
}
}
和var_dump($country_tabs):
我想要实现的是使我的代码不显示相同的国家!
array\u unique($country\u tabs)不起作用,因为我在输出中有更多的数组。您最好在数据库查询中获得以下唯一值:
SELECT DISTINCT country FROM blahs WHERE country IS NOT NULL ORDER BY country
请注意,我添加了DISTINCT关键字以执行您希望执行的操作。修改您的查询以仅获取唯一的国家/地区:
$get_countries = "SELECT DISTINCT country FROM blahs WHERE country IS NOT NULL ORDER BY country";
使用
从国家不为空的blahs中按国家顺序选择不同的国家
它将删除所有重复的值。通过mysql查询获取唯一的国家:
SELECT country
FROM blahs
WHERE country IS NOT NULL
GROUP BY country
ORDER BY country
这是更优化的方式 应该有用。该
var\u dump()
是执行array\u unique()
后的输出?它只对一个数组有效!我曾尝试将所有这些阵列都放在一个阵列中,但我没有达到我想要的效果,我做得不正确。
$get_countries = "SELECT DISTINCT country FROM blahs WHERE country IS NOT NULL ORDER BY country";
SELECT country
FROM blahs
WHERE country IS NOT NULL
GROUP BY country
ORDER BY country