我一直试图在array_unique没有成功的情况下内爆 2 个变量。这两个变量进行 mysql 调用,每个变量都返回 data ok。放在一个数组中也会返回来自两者的数据,但是,当array_unique它们内爆时,它们有重复的数据,我想在返回时清除这些数据。建议将不胜感激。
$a = 'mysql string 1';// basically words A, B, C
$b = 'mysql string 2';// basically words D, A, E
$a_b_array = array($a, $b);
sort($a_b_array);
$a_b_string = implode("\n", array_unique($a_b_array));
echo $a_b_string; //returns $a and $b with duplicated data
Run Code Online (Sandbox Code Playgroud)
我也试过SORT_REGULAR,不要认为这是问题所在:
//sort($a_b_array);
$a_b_string = implode("\n", array_unique($a_b_array, SORT_REGULAR);
Run Code Online (Sandbox Code Playgroud)
预期结果为:A、B、C、D、E
这是一个问题.我正在通过数组中的新行爆炸字符列表.并在其上做独特的数组.但它没有按预期工作.下面是代码:
$list = "test
ok
test
test
ok
ok
test";
$list_explode = explode("\n", $list); //exploding the characters of the list from the input
//displaying unique
array_map('trim', $list_explode);
$result = array_unique($list_explode);
print_r($result);
Run Code Online (Sandbox Code Playgroud)
结果是
Array ( [0] => test [1] => ok [6] => test )
在下面的代码中:
$sth = $dbh->query('SELECT DISTINCT title,courseId,location from training');
$sth->setFetchMode(PDO::FETCH_ASSOC);
$results = $sth->fetchAll();
$uu = array_unique($results);
echo "<pre>";
print_r($uu);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
我只得到1个结果 print_r($uu);
如果我删除array_unique所有(30+)行返回.(不,并非所有这些都是重复的):)
我究竟做错了什么?
编辑var_dump()结果:
array(23) {
[0]=>
array(3) {
["title"]=>
string(26) "String Here"
["courseId"]=>
string(1) "8"
["location"]=>
string(1) "1"
}
[1]=>
array(3) {
["title"]=>
string(26) "Another String Here"
["courseId"]=>
string(1) "8"
["location"]=>
string(1) "2"
}
[2]=>
array(3) {
["title"]=>
string(24) "Third String Here"
["courseId"]=>
string(1) "5"
["location"]=>
string(1) "2"
}
Run Code Online (Sandbox Code Playgroud)
等等...
大家好,我试图从该数组中查找重复的x值并将其删除,只保留唯一的x值。例如我的数组是
Array
(
[0] => Array
(
[x] => 0.5
[y] => 23
)
[1] => Array
(
[x] => 23
[y] => 21.75
)
[2] => Array
(
[x] => 14.25
[y] => 21.875
)
[3] => Array
(
[x] => 19.375
[y] => 21.75
)
[4] => Array
(
[x] => 9.125
[y] => 21.875
)
[5] => Array
(
[x] => 23
[y] => 19.625
)
[6] => Array
(
[x] => 19.375
[y] => 19.625
) …Run Code Online (Sandbox Code Playgroud) 有没有办法将两个或多个数组放入array_unique()函数?如果没有,是否有其他解决方案可以从多个阵列获得独特的结果?
我想从数组中删除重复的值.我知道使用array_unique(array)函数但在foreach循环中遇到问题.这不是一个重复的问题,因为我已经阅读了几个有关此问题的问题,其中大多数都强制使用array_unique(array)函数,但我不知道在foreach循环中使用它.这是我的PHP功能.
$images = scandir($dir);
$listImages=array();
foreach($images as $image){
$listImages=$image;
echo substr($listImages, 0, -25) ."<br>"; //remove last 25 chracters
}
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
我想从php中删除数组中的重复元素.以下是数组的结构
Array
(
[0] => Array
(
[0] => xllga@hotmail.com
[1] => bounce@abc.com
[2] => 20120416135504.21734.qmail@abc.com
[3] => xllga@hotmail.com
[4] => info@abc.com
[5] => info@abc.com
[6] => xllga@hotmail.com
[7] => xllga@hotmail.com
)
)
Run Code Online (Sandbox Code Playgroud)
怎么去呢?
我有一个完整的董事名单,但只想显示其唯一的名字,不要重复。看起来像
导演A,导演B,导演C ...
不
导演A,导演A,导演B,导演C,导演C,...
我尝试使用array_unique做到这一点,但似乎没有在数组中放入任何数据。
我看到foreach循环显示了所有Director的名称,但是数组$ alldirectors却保持为空。
这是我正在使用的代码。
<?php
$resume = get_posts(array(
'post_type' =>'resume',
'numberposts'=>-1,
'meta_key' => 'director',
'meta_value' => ''
));
$alldirectors = array();
foreach( $resume as $post ) {
$director = get_post_meta( $post->ID, 'director', true );
}
$directors = array_unique($alldirectors);
foreach ($directors as $director) {
echo $directors;
}
?>
Run Code Online (Sandbox Code Playgroud)
我可能想念的很简单,但是我是php和wordpress的新手。感谢您的所有帮助。
我array_unique在数组上使用后,当我var_dump在数组中时,它仍然具有相同的内容,并带有重复项:
array(21) {
[0]=> string(10) "tricou_CRS"
[1]=> string(10) "tricou_CRM"
[2]=> string(11) "tricou_CRXL"
[3]=> string(10) "tricou_CBM"
[4]=> string(10) "tricou_CBL"
[5]=> string(10) "tricou_CWS"
[6]=> string(11) "tricou_CWXL"
[7]=> string(10) "tricou_CRS"
[8]=> string(10) "tricou_CRM"
[9]=> string(11) "tricou_CRXL"
[10]=> string(10) "tricou_CBM"
[11]=> string(10) "tricou_CBL"
[12]=> string(10) "tricou_CWS"
[13]=> string(11) "tricou_CWXL"
[14]=> string(10) "tricou_CRS"
[15]=> string(10) "tricou_CRM"
[16]=> string(11) "tricou_CRXL"
[17]=> string(10) "tricou_CBM"
[18]=> string(10) "tricou_CBL"
[19]=> string(10) "tricou_CWS"
[20]=> string(11) "tricou_CWXL" }
Run Code Online (Sandbox Code Playgroud)
这显然有一些重复.现在,没有太多的代码可以帮助,真的,因为它只是array_unique($myarr);var_dump($myarr);.那么,我在那里错过了什么?array_unique不应该删除重复项吗?类型和内容在许多阵列的位置都是相同的.
我在 php 中有一个数组,如下所示
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Run Code Online (Sandbox Code Playgroud)
现在我想删除父索引 0,1... 并最终想要获取所有值(只有唯一值)。
谢谢。
我正在尝试创建一个名为 filer_out 的方法!它接受一个数组和一个 proc,并返回相同的数组,但每个元素在通过 proc 时都返回 true,但需要注意的是我们不能使用 Array#reject!
我是这样写的:
def filter_out!(array, &prc)
array.each { |el| array.delete(el) if prc.call(el)}
end
arr_2 = [1, 7, 3, 5 ]
filter_out!(arr_2) { |x| x.odd? }
p arr_2
Run Code Online (Sandbox Code Playgroud)
但是当我运行代码时,打印出来的是:
[7, 5]
Run Code Online (Sandbox Code Playgroud)
即使答案应该是:
[]
Run Code Online (Sandbox Code Playgroud)
在查看解决方案后,我看到首先使用了 Array#uniq:
def filter_out!(array, &prc)
array.uniq.each { |el| array.delete(el) if prc.call(el) }
end
arr_2 = [1, 7, 3, 5 ]
filter_out!(arr_2) { |x| x.odd? }
p arr_2
Run Code Online (Sandbox Code Playgroud)
并显示了正确的输出:
[]
Run Code Online (Sandbox Code Playgroud)
所以我想我的问题是,为什么必须使用 Array#uniq 才能获得正确的解决方案?感谢您的帮助!
我有一个数组$ tmp:
Array
(
[0] => 49
[1] => 49
[2] => 49
)
Run Code Online (Sandbox Code Playgroud)
使用后array_unique($tmp)我得到这个输出:
Array
(
[0] => 49
[1] => 49
[2] => 49
)
Run Code Online (Sandbox Code Playgroud)
而且我想得到
Array
(
[0] => 49
)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我是PHP新手
array-unique ×12
php ×11
arrays ×7
duplicates ×2
foreach ×1
implode ×1
proc ×1
ruby ×1
wordpress ×1