foreach ($keyword as $val){ $sql="Select title from XXXX title like '%$val%' limit 10 ";
$list = array();
$dsql->SetQuery($sql);
$dsql->Execute();
while ($row = $dsql->GetArray()) {
$list[] = $row;
}
}
结果是这样的。 请问怎么合并 并去除重复。。
Array
(
[0] => Array
(
[title] => 1111111111
) [1] => Array
(
[title] => 22222222222
) [2] => Array
(
[title] =>333333333333
)
)
Array
(
[0] => Array
(
[title] => 1111111111
)
)
$list = array();
$dsql->SetQuery($sql);
$dsql->Execute();
while ($row = $dsql->GetArray()) {
$list[] = $row;
}
}
结果是这样的。 请问怎么合并 并去除重复。。
Array
(
[0] => Array
(
[title] => 1111111111
) [1] => Array
(
[title] => 22222222222
) [2] => Array
(
[title] =>333333333333
)
)
Array
(
[0] => Array
(
[title] => 1111111111
)
)
$list = array();
foreach ($keyword as $val){
$sql="Select title from XXXX title like '%$val%' limit 10 ";
$dsql->SetQuery($sql);
$dsql->Execute();
$array = array();
while ($row = $dsql->GetArray()) {
array_push($array,$row);
}
$list[] = $array;
}
print_r($list);
array_unique() 接受 array 作为输入并返回没有重复值的新数组。注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。Note: 当且仅当 (string) $elem1 === (string) $elem2 时两个单元被认为相同。就是说,当字符串的表达一样时。 第一个单元将被保留。Example #1 array_unique() 例子
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
以上例程会输出:
Array
(
[a] => green
[0] => red
[1] => blue
)
Example #2 array_unique() 和类型
<?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?>
以上例程会输出:
array(2) {
[0] => int(4)
[2] => string(1) "3"
}
I came across one limitation of array_unique: it doesn't work properly if you have arrays inside your main array.The reason is that to compare two values, the function tests if (string) $value1 == (string) $value2. So if $value1 and $value2 are both arrays, the function will evaluate the test to 'Array' == 'Array', and decide that the $values are repeated even if the arrays are different.So a work around is to find a better conversion of an array to a string, which can be done with json:<?php
print "define an array with repeated scalar '1' and repeated 'array(1)':";
$a_not_unique = array(
'a' => 1,
'b' => 1,
'c' => 2,
'd' => array(1),
'e' => array(1),
'f' => array(2),
);
print_r($a_not_unique);print "try to use simply array_unique, which will not work since it exludes 'array(2)':";
$a_unique_wrong = array_unique($a_not_unique);
print_r($a_unique_wrong);print "convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':";
$a_unique_right = $a_not_unique;
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_encode($value);'));
$a_unique_right = array_unique($a_unique_right);
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_decode($value, true);'));
print_r($a_unique_right);
?>Results:
define an array with repeated scalar '1' and repeated 'array(1)':
Array
(
[a] => 1
[b] => 1
[c] => 2
[d] => Array
(
[0] => 1
) [e] => Array
(
[0] => 1
) [f] => Array
(
[0] => 2
)
)try to use simply array_unique, which will not work since it exludes 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
)convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
) [f] => Array
(
[0] => 2
)
)
foreach ($keyword as $val){
$sql="Select title from XXXX title like '%$val%' limit 10 ";
$list = array();
$dsql->SetQuery($sql);
$dsql->Execute();
while ($row = $dsql->GetArray()) {
$list[] = $row['title'];
}
}
$list = array_unique($list);如果是多值,可写作
foreach ($keyword as $val){
$sql="Select * from XXXX title like '%$val%' limit 10 ";
$list = array();
$dsql->SetQuery($sql);
$dsql->Execute();
while ($row = $dsql->GetArray()) {
$list[$row['title']] = $row;
}
}
$list = array_values($list);
foreach ($keyword as $val){
$sql="Select title from XXXX title like '%$val%' limit 10 ";
$dsql->SetQuery($sql);
$dsql->Execute();
$array = array();
while ($row = $dsql->GetArray()) {
$row = array_unique($row); //去除重复
array_push($array,$row);
}
$list[] = $array;
}
print_r($list);具体看你想要什么样的结果,怎么方便使用这个结果数组。你不妨将上面的数组结果打出来看看,哪个是你更需要的。