2015-02-24 43 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