0

My output is like this:

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 
)

) `

How can I check if in_array(Array[position]=='main-header' Without using foreach?

This is my code:

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 Answer 1

3

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column():

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

Or with array_map:

if(in_array('main-header', array_map(function($v) { return $v['position']; }, $array))) {
    //found
}
Sign up to request clarification or add additional context in comments.

6 Comments

My php version is 5.4.35
Added another option.
This just returns "1". What if found on multiple arrays? foreach is the only way? What i wanted is to find the arrays that in sub_array 'position' is the specific "title" and return the array values, or return to a variable
Totally different question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.