假设我有2个阵列:
$array1 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'bbb' [count] => '456' )
[2] => Array ( [user] => 'ccc' [count] => '789' ) );
$array2= ( [0] => aaa)
[1] => ccc );
我想从第一个数组中的第二个数组中搜索值,并创建第三个包含第一个数组中所有元素的数组,如下所示:
$array3 =( [0] => Array ( [user] => 'aaa' [count] => '123' )
[1] => Array ( [user] => 'ccc' [count] => '789' ) );
请帮忙。先感谢您 (抱歉英语不好)
答案 0 :(得分:0)
$array1 = array(
array( 'user' => 'aaa', 'count' => 123 ),
array( 'user' => 'bbb', 'count' => 456 ),
array( 'user' => 'ccc', 'count' => 789 ),
);
$array2 = array('aaa', 'ccc');
var_dump(array_filter($array1, function($el) use($array2) {
return in_array($el['user'], $array2);
}));
它会保留密钥,您可以根据需要重置密钥。或者使用foreach
循环
$out = array();
foreach($array1 as $el)
{
if (in_array($el['user'], $array2))
$out[] = $el;
}
var_dump($out);
答案 1 :(得分:0)
可以尝试使用foreach()
。这里的例子......
$array3 = array();
foreach($array2 as $search){
foreach($array1 as $val){
if($search == $val['user']){
$array3[] = $val;
}
}
}
print_r($array3);