我有这个
$array1 = ['apple'];
$array2 = ['apple', 'orange'];
我想检查array1中是否有来自array2的任何值并返回该值。我试过in_array($array1, $array2);
它只返回true或false。
答案 0 :(得分:7)
您正在寻找array_intersect()
$array1 = ['apple'];;
$array2 = ['apple', 'orange'];
$result = array_intersect($array1, $array2);
print_r($result);
答案 1 :(得分:1)
有一个函数(define-generic-mode
'foo-mode ;; name of the mode to create
'("#") ;; comments start with '!!'
'("for" "if" "else" ) ;; some keywords
'(("=" . 'font-lock-operator) ;; '=' is an operator
("+" . 'font-lock-operator) ;; '=' is an operator
("-" . 'font-lock-operator) ;; '=' is an operator
("<-" . 'font-lock-operator) ;; '=' is an operator
("*" . 'font-lock-operator) ;; '=' is an operator
("/" . 'font-lock-operator) ;; '=' is an operator
("," . 'font-lock-builtin) ;; ';' is a built-in
(";" . 'font-lock-builtin)) ;; ';' is a built-in
'("\\.myext$") ;; files for which to activate this mode
'((lambda () (modify-syntax-entry ?' "\""))) ;; other functions to call
"A generic mode for myext files" ;; doc string for this mode
)
使用
array_intersect()
array_intersect()比较两个数组的值,并返回匹配
答案 2 :(得分:1)
in_array会返回true
或false
,所以当它为真时你可以将它添加到数组中:
$array1 = ['apple'];
$array2 = ['apple', 'orange', 'apple'];
$result = [];
foreach ($array2 as $a2) {
if (in_array($a2, $array1)) {
$result[] = $a2;
}
}
print_r(array_unique($result));