首先,我有一个多维数组,如下所示:
$array=array (0 => array (0 => '',),1 => array (0 => 'sample.com',1 => 'test.com',2 => 'check.com',3 =>'rack.com',),2 => array (0 => '12345.34535',1 => '243.345345.4535',2 => '3453.534534',3 => '45.453453',),3 => array (0 => '978.797',1 => '789.7897997.7897',2 => '97897.78979.798',3 => '78978979',),4 => NULL,5 => NULL,6 => NULL,7 => NULL,8 => NULL,9 => NULL,10 => NULL,11 => NULL,12 => NULL,);
我有一个用户在变量中选择的选项:
$options=array (0 => 'blreferrer',1 => 'ipwl',2 => 'ipbl',);
我使用下面的代码从用户选择的多维数组中获取正确的密钥
$options1 = array(0=>"wlreferrer",1=>"blreferrer",2=>"ipwl",3=>"ipbl",4=>"geowl",5=>"geobl",6=>"languagewl",7=>"languagebl",8=>"browserwl",9=>"browserbl",10=>"oswl",11=>"osbl",12=>"viscount");
$option2 = (array_intersect($options1, $options));
$option3 = (array_keys($option2));
以下代码用于搜索第一个多维数组
$option4 = array();
$referrer = 'sample.com';
$ip = '789.7897997.7897';
$option5 = array(0=>$referrer,1=>$referrer,2=>$ip,3=>$ip,4=>$geo,5=>$geo,6=>$language,7=>$language,8=>$browser,9=>$browser,10=>$os,11=>$os,12=>$viscount);
最后我尝试搜索
foreach ($option3 as $key) {
if (in_array($option5[$key], $array[$key])) {
array_push($option4, "0");
} else {
array_push($option4, "1");
}
}
当我打印$ option4时,我得到以下结果,它应该是0,1,0 ..
Array ( [0] => 1 [1] => 1 [2] => 1 )
我认为问题源于在多维数组上使用in_array,但无法找到任何解决方案。
答案 0 :(得分:0)
您可以使用array_filter进行此搜索。例如:
$example_array = [your array];
$reference_array = [the values to be searched];
$filtered = array_filter($example_array,function($element)use($reference_array){
foreach($element as $index=>$value){
if(in_array($value,$reference_array)){
return true;
}
}
});