我的输出是这样的:
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'];
}
}
}
答案 0 :(得分:3)
PHP> = 5.5.0或使用PHP Implementation of array_column():
array_column()
或if(in_array('main-header', array_column($array, 'position'))) {
//found
}
:
array_map