2015-02-24 40 views
0

我的输出是这样的:如何从多个数组中获取in_array值?

Array ( 
[0] => Array (
[type] => 1 
[position] => main-widgets 
[key_position] => 3 
[code] => template/portfolio.inc 
) 
[1] => Array (
[type] => 2 
[position] => main-widgets 
[key_position] => 3 
[code] => This is a code 
) 

)`

我怎么能如果in_array(Array[position]=='main-header'不使用foreach检查?

这是我的代码:

if(in_array('main-header', array_column($PAGE['templates'], 'position'))) { 
    $count = array_count_values($PAGE['templates']['position']); 
    if($count['main-header']>1){ 
    echo 'multiple'; 
    foreach($PAGE['templates'] as $pos){ 
     if($pos['type'] == 1){ 
      require $base['basepath'].$pos['code']; 
     }else { 
      echo $pos['code']; 
     } 
    } 
} else { 
     echo 'Only 1'; 
     if($PAGE['templates']['type'] == 1){ 
      require $base['basepath'].$PAGE['templates']['code']; 
     }else { 
      echo $PAGE['templates']['code']; 
     } 
    } 
} 

回答

3

PHP> = 5.5.0需要array_column()或使用PHP Implementation of array_column()

if(in_array('main-header', array_column($array, 'position'))) { 
    //found 
} 

或者与array_map

if(in_array('main-header', array_map(function($v) { return $v['position']; }, $array))) { 
    //found 
} 
+0

我的PHP版本是5.4.35 – DecoderNT 2015-02-24 21:35:32

+0

确定从https://github.com/ramsey/ar下载了该类ray_column/blob/master/src/array_column.php – DecoderNT 2015-02-24 21:36:52

+0

增加了另一个选项。 – AbraCadaver 2015-02-24 21:45:51