3

Ok, so I have this array :-

0 => 
    array (size=2)
      'receiver_telmob' => string '0707105396' (length=10)
      0 => string '0707105396' (length=10)
  1 => 
    array (size=2)
      'receiver_telmob' => string '0704671668' (length=10)
      0 => string '0704671668' (length=10)
  2 => 
    array (size=2)
      'receiver_telmob' => string '0707333311' (length=10)
      0 => string '0707333311' (length=10)

And I'm trying to search in this array using in_array. But, I never get any true value. Here's what I'm trying to do:-

$searchnumber = '0707333311';
if(in_array($searchnumber,$arrayAbove))
{ 
//do something
}

But the if always results a false output. I guess that I'm not using the in_array correctly here. What should I correct to make it work? Thanks.

1
  • 1
    You cannot use in_array for multidimensional arrays. Commented Nov 4, 2014 at 19:47

5 Answers 5

2
$array = array(
   "0" => array(
      "receiver_telmob" => "0707105396",
      "0"               => "0707105396"
   ),
   "1" => array(
      "receiver_telmob" => "0704671668",
      "0"               => "0704671668"
   ),
   "2" => array(
      "receiver_telmob" => "0707333311",
      "0"               => "0707333311"
   )
);

$searchnumber = "0707333311";

foreach($array as $v) {
   if ($v['receiver_telmob'] == $searchnumber) {
      $found = true;
   }
}

echo (isset($found) ? 'search success' : 'search failed');
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

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

$searchnumber = '0707333311';
if(in_array_recursive($searchnumber,$arrayAbove))
{ 
   //do something
}

Comments

0

in_array() doesn't work with multi-dimensional arrays. You need something like this -

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

Then you could do this -

$searchnumber = '0707333311';
if(in_multi_array($searchnumber,$arrayAbove))
{ 
//do something
}

2 Comments

is that in the manual somewhere? someone else posted this exact same function.
I've been using this for a long time for multidimensional arrays. Heck I don't even remember how I cobbled it together, just copied and pasted from PHP code I have in a project.
0

You can't use in_array for multidimensional arrays!

But this function should work for you:

<?php
    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;
    }

?>

Then you can use it like this:

echo in_array_r("0707333311", $arrayAbove) ? 'true(found)' : 'false(not found)';

2 Comments

you could simplify that a bit: return (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict)));
@Adelphia Yes true!, but for me it would be to nested.
0

You would have to use in_array for each sub array.

So if you have a 1 dimensional array like

[1,4,43,2,5,4] you could call in_array but when you have multidimensional you have to iterate over the top dimension and call in_array

 for($i = 0;$i < arr.count(); $i++){
    if(in_array($searchnum, $arr[$i]){
     //do something
    }
 }

NOTE: the example above only works for 2d arrays just to demonstrate what I was talking about

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.