doumei1908 2012-04-02 02:17
浏览 38

in_array多维数组搜索

To begin I have a multi-dimensional array as seen below:

$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,);

I have the options the user selected in a variable:

$options=array (0 => 'blreferrer',1 => 'ipwl',2 => 'ipbl',);

I use the below code to get the correct keys from the multidimensional array form the users selection

$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));  

The below code is utilized for searching the first multidimensional array

$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);

Finally I try and search

foreach ($option3 as $key) {

if (in_array($option5[$key], $array[$key])) {

array_push($option4, "0");

} else {

array_push($option4, "1");

}

}

When I print out $option4 I get the below results when it should be 0,1,0..

 Array ( [0] => 1 [1] => 1 [2] => 1 )

I believe the problem stems from using in_array on a multi-dimensional array however haven't been able to find any solutions.

  • 写回答

1条回答 默认 最新

  • du13932014807 2012-04-02 02:26
    关注

    you can use array_filter for this search. For example:

    $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;
            }
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。