I have an array similar to this:

array(2) {
  [0]=>
  array(2) {
    ["code"]=>
    string(2) "en"
    ["name"]=>
    string(7) "English"
  }
  [1]=>
  array(2) {
    ["code"]=>
    string(2) "bg"
    ["name"]=>
    string(9) "Bulgarian"
  }
}

How do I check if the string Bulgarian is part of the above array, or alternatively if the lang code 'en' is part of the array? It would be great if I didn't have to use foreach to loop through the entire array and compare the string with each element['code'] or element['name'].

share|improve this question

78% accept rate
3  
What have you tried , have you tried any code ? if so please post it in your question, so you can be helped. – Haroon Jun 13 '12 at 7:13
3  
In some form or another you'll always need a loop. Even in_array loops internally. – deceze Jun 13 '12 at 7:14
3  
" It would be great if I didn't have to use foreach to loop through the entire array and compare the string with each element" - then how would you find what you're looking for? – CodeCaster Jun 13 '12 at 7:14
@deceze, true. But that doesn't mean I'd advocate custom foreach loops over in_array() (where applicable of course). – Jason McCreary Jun 13 '12 at 7:15
feedback

3 Answers

up vote 4 down vote accepted
// $type could be code or name
function check_in_array($arr, $value, $type = "code") {
  return count(array_filter($arr, function($var) use ($type, $value) {
    return $var[$type] === $value;
  })) !== 0;
}
share|improve this answer
This code looks like throwing undefined variable warnings all over the place. I wonder how TS got this to work. Also you should do an early exit if found. There is no need to count higher than one. – hakre Jun 13 '12 at 10:18
@hakre Yep, that's so. – xdazz Jun 13 '12 at 10:49
Another var was missing, still wondering about early exit, I'd say foreach ain't bad here. – hakre Jun 13 '12 at 10:51
@hakre Yep, foreach is better here, just show the way other than it – xdazz Jun 13 '12 at 10:54
My experience is this works in PHP 5.3.1 but not in 5.2.17 – twnaing Dec 18 '12 at 20:48
feedback

I know my code used foreach but it is easy to understand and use.

$language=array();
$language[0]['code']='en';
$language[0]['name']='English';
$language[1]['code']='bg';
$language[1]['name']='Bulgaria';

var_dump(my_in_array('Bulgaria', $language));

function my_in_array($search, $array) {
    $in_keys = array();
    foreach($array as $key => $value){
        if(in_array($search, $value)){
            $in_keys[]=$key;
        }
    }
    if(count($in_keys) > 0){
        return $in_keys;
    }else{
        return false;
    }
}
share|improve this answer
feedback

You can use this function for recursive search

function in_arrayr($needle, $haystack) {
        foreach ($haystack as $v) {
                if ($needle == $v) return true;
                elseif (is_array($v)) return in_array($needle, $v);
        }
        return false;
} 

Or you can use json_encode on your array and search occurence of substring :)

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.