如何从多个数组中获取in_array值?

时间:2015-02-24 21:30:29

标签: php arrays

我的输出是这样的:

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'];
        }
    }
}

1 个答案:

答案 0 :(得分:3)

{p <1}}需要

PHP&gt; = 5.5.0或使用PHP Implementation of array_column()

array_column()

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

array_map