2

I have this dynamic array, generate after a submit $_POST.

Array
(
    [0] => Array
        (
            [0] => lng_criteria_balance
            [1] => Array
                (
                    [0] => lng_type_balance_positive
                )

        )

    [1] => Array
        (
            [0] => lng_criteria_sex
            [1] => Array
                (
                    [0] => F
                )

        )

    [2] => Array
        (
            [0] => lng_criteria_note
            [1] => Array
                (
                    [0] => cane
                )

        )

)

Array is variable, and it's key also. I need to search if a specified value exists. I did try this but

<?php
 if (in_array('lng_criteria_balance', $args))
 {
   echo 'found!';
 }
 else
 {
   echo 'not found :(';
 }

But it prints "not found". Thank you.

PS I could check with a foreach loop, but I would not use it (for best performance)

3
  • 3
    in_array searches in just one array not a multidimensional array. Commented Jan 19, 2015 at 11:06
  • Is it always in 0-th index? Or it can be on any level in any key? Commented Jan 19, 2015 at 11:07
  • For best performance, I think your array should look like: Array ( [lng_criteria_balance] => lng_type_balance_positive, [lng_criteria_sex] => F, [lng_criteria_note] => cane, ). It also simplifies your code that uses it and makes possible the usage of the PHP standard functions in_array(), array_key_exists(), array_search(), isset() a.s.o. Commented Jan 19, 2015 at 11:13

4 Answers 4

3

For multi-dimension array you need to check it recursively.

Do like this:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

Output:

echo in_array_r("lng_criteria_balance", $your_array_variable) ? 'found' : 'not found';
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, because in your array, there are numeric keys only. Use foreach to iterate through the subarrays, and search in that.

$inArray = false;
foreach ($array as $key => $subarray) {
    if (in_array('needle', $subarray)) {
        $inArray = true;
        break;
    }

}

Comments

1

Try this...

<?php
$arr = array(0 => array("id"=>1,"temp"=>"lng_criteria_balance"),
             1 => array("id"=>2,"temp"=>"test"),
             2 => array("id"=>3,"temp"=>"test123")
);
function search_in_array($srchvalue, $array)
{
    if (is_array($array) && count($array) > 0)
    {
        $foundkey = array_search($srchvalue, $array);
        if ($foundkey === FALSE)
        {
            foreach ($array as $key => $value)
            {
                if (is_array($value) && count($value) > 0)
                {
                    $foundkey = search_in_array($srchvalue, $value);
                    if ($foundkey != FALSE)
                        return $foundkey;
                }
            }
        }
        else
            return $foundkey;
    }
}
if(!empty(search_in_array('lng_criteria_balance',$arr)))
{
   echo 'found!';
 }
 else
 {
   echo 'not found :(';
 }
?>

Comments

1
function multi_in_array_r($needle, $haystack) {
     if(in_array($needle, $haystack)) {
          return true;
     }
     foreach($haystack as $element) {
          if(is_array($element) && multi_in_array_r($needle, $element))
               return true;
     }
   return false;
}

Comments

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.