user2619381
user2619381

Reputation: 217

Using in_array for multidimensional arrays

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.

Upvotes: 2

Views: 17656

Answers (5)

TURTLE
TURTLE

Reputation: 3847

$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');

Upvotes: 2

makhov
makhov

Reputation: 450

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
}

Upvotes: 0

Jay
Jay

Reputation: 2686

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

Upvotes: 0

Rizier123
Rizier123

Reputation: 59681

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)';

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34416

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
}

Upvotes: 0

Related Questions