我知道这个问题之前曾有人提出过,或者可能是我找不到与整个网站有关的问题和答案。对于数组中重复的值的删除过程,我需要一些建议。这是我的代码:
$newarray = array();
foreach($rows as $item) {
if (!in_array($rows, $newarray)) {
$newarray[$item['country_name']]['cityName'][] = $item['city_name'];
}
$newarray[$item['country_name']]['hotelName'][] = $item['hotel_name'];
}
echo '<pre>';
print_r($newarray);
echo '</pre>';
这是我的输出:
Array (
[India] => Array
(
[cityName] => Array
(
[0] => Gurugram
[1] => Trivandrum
[2] => Bengaluru
[3] => Udaipur
[4] => Salcette
[5] => Mumbai
[6] => New Delhi
[7] => Chennai
[8] => Salcette
[9] => Salcette
[10] => Bengaluru
[11] => Chennai
[12] => New Delhi
[13] => Udaipur
[14] => Delhi
[15] => Jaipur
[16] => Jaipur
)
如您所见,返回值中有多个重复值。我需要这样的东西:
Array (
[India] => Array
(
[cityName] => Array
(
[0] => Gurugram
[1] => Trivandrum
[2] => Bengaluru
[3] => Udaipur
[4] => Mumbai
[5] => Chennai
[6] => Salcette
[7] => Chennai
[8] => New Delhi
[9] => Udaipur
[10] => Delhi
[11] => Jaipur
)
答案 0 :(得分:0)
希望,这会有所帮助。
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
结果看起来像
Array ( [a] => red [b] => green )
在这里,我仅举一个简单的例子。我们可以使用 array_unique() PHP函数
删除数组中的重复值答案 1 :(得分:0)
您的IF语句中有错误。改变这个:
if (!in_array($rows, $newarray)) {
$newarray[$item['country_name']]['cityName'][] = $item['city_name'];
}
对此:
if (!isset($newarray[$item['country_name']]['cityName']) || !in_array($item['city_name'], $newarray[$item['country_name']]['cityName'])) {
$newarray[$item['country_name']]['cityName'][] = $item['city_name'];
}
答案 2 :(得分:0)
您可以为此使用 array_unique 函数
$newarray = array();
foreach($rows as $item) {
if (!in_array($rows, $newarray)) {
$newarray[$item['country_name']]['cityName'][] = $item['city_name'];
}
// because of you array is multidimensional
// you can use array_unique function with array_map searching
$newarray= array_map("unserialize", array_unique(array_map("serialize",$newarray)));
$newarray[$item['country_name']]['hotelName'][] = $item['hotel_name'];
}
echo '<pre>';
print_r($newarray);
echo '</pre>';
答案 3 :(得分:0)
将$ rows in更改为$ item ['city_name']并尝试:
ds = gdal.Open('tif_file.tif', gdal.GA_ReadOnly)
rb = ds.GetRasterBand(1)
img_array = rb.ReadAsArray()
答案 4 :(得分:0)
修复后 试试这个:)
$newarray = array();
foreach($rows as $item) {
$newarray[$item['country_name']]['cityName'][] = $item['city_name'];
if(count($newarray[$item['country_name']]['cityName']) > 1)
{
$temp_array = array_values($newarray[$item['country_name']]['cityName']);//get values and assign it in new array
$temp_array = array_unique($newarray[$item['country_name']]['cityName']);// remove duplicate elements
$temp_array = array_filter($newarray[$item['country_name']]['cityName']);// remove blank elements from array - must use after merging or unique
$newarray[$item['country_name']]['cityName'] = $temp_array
}
$newarray[$item['country_name']]['hotelName'][] = $item['hotel_name'];
}
echo '<pre>';
print_r($newarray);
echo '</pre>';