[英]array_unique with two arrays
I have two arrays of the same length ($search_type, $search_term)
. 我有两个相同长度的数组
($search_type, $search_term)
。 I want to remove any duplicates in the sense of there being searches that have the same type and search term (ie $search_type[$a] == $search_type[$b] && $search_term[$a] == $search_term[$b]
). 我想从具有相同类型和搜索词的搜索(即
$search_type[$a] == $search_type[$b] && $search_term[$a] == $search_term[$b]
)。
I'm aware that I could write this using loops, was wondering if there's a simpler (but equally efficient) way that takes advantage of built in functions? 我知道我可以使用循环来编写此代码,想知道是否存在一种更简单(但同样有效)的方法来利用内置函数?
尝试使用array_intersect() (它将获取两个数组)并返回一个数组,其中数组A中的值存在于数组B中。或者尝试array_diff()进行相反的操作,并返回A中所有不存在于B中的值的数组。
Ok, here's an overly complex ad-hoc function foo()
(lacking error handling, documentation and testing) that combines those two arrays. 好的,这是一个过于复杂的临时
function foo()
(缺少错误处理,文档和测试),将这两个数组结合在一起。 array_unique() takes care of the duplicates. array_unique()负责重复项。
<?php
$search_term = array('termA', 'termB', 'foo'=>'termC', 'bar'=>'termD', 'termB', 'termA');
$search_type= array('typeA', 'typeB', 'foo'=>'typeC', 'bar'=>'typeD', 'typeB', 'typeA');
$x = foo(array('term', $search_term), array('type', $search_type));
$x = array_unique($x, SORT_REGULAR);
var_dump($x);
function foo() {
$rv = array();
$params = func_get_args();
foreach ( array_keys($params[0][1]) as $key ) {
$rv[$key] = array();
foreach( $params as $p ) {
$rv[$key][$p[0]] = $p[1][$key];
}
}
return $rv;
}
prints 版画
array(4) {
[0]=>
array(2) {
["term"]=>
string(5) "termA"
["type"]=>
string(5) "typeA"
}
[1]=>
array(2) {
["term"]=>
string(5) "termB"
["type"]=>
string(5) "typeB"
}
["foo"]=>
array(2) {
["term"]=>
string(5) "termC"
["type"]=>
string(5) "typeC"
}
["bar"]=>
array(2) {
["term"]=>
string(5) "termD"
["type"]=>
string(5) "typeD"
}
}
It doesn't look like there's a simple way to solve the problem using just built-in functions. 看起来似乎没有简单的方法可以仅使用内置函数来解决问题。
This (at least logically) should work. 这(至少在逻辑上)应该起作用。
$search_terms = array('a', 'b', 'c', 'c', 'd', 'd');
$search_types = array( 1, 2, 3, 4, 5, 5);
$terms = array_fill_keys($search_terms, array());
// Loop through them once and make an array of types for each term
foreach ($search_terms as $i => $term)
$terms[$term][] = $search_types[$i];
// Now we have $terms = array('a' => array(1),
// 'b' => array(2),
// 'c' => array(3, 4),
// 'd' => array(5, 5)
// );
// Now run through the array again and get rid of duplicates.
foreach ($terms as $i => $types)
$terms[$i] = array_unique($types);
Edit: Here's a shorter and presumably more efficient one where you end up with a less pretty array: 编辑:这是一个较短且可能更有效的数组,最终得到了一个不太漂亮的数组:
$search_terms = array('a', 'b', 'c', 'c', 'd', 'd');
$search_types = array( 1, 2, 3, 4, 5, 5);
$terms = array_fill_keys($search_terms, array());
// Loop through them once and make an array of types for each term
foreach ($search_terms as $i => $term)
$terms[$term][$search_types[$i]] = 1;
// Now we have $terms = array('a' => array(1 => 1),
// 'b' => array(2 => 1),
// 'c' => array(3 => 1, 4 => 1),
// 'd' => array(5 => 1)
// );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.