0

$record_record contains:

Array
(
    [0] => Array
        (
            [id] => 252
            [origin] => laptop.me.
        )

    [1] => Array
        (
            [id] => 255
            [origin] => hello.me.
        )

    [2] => Array
        (
            [id] => 254
            [origin] => intel.me.
        ) 
)

I need to search if 255 is exist in the array. The code below didn't work.

if (in_array('255', $record_record, true)) {
    echo "'255' found with strict check\n";
}
else {
     echo "nope\n";
} 

I had a feeling because it's a nested array the function will not work. Help me please?

hakre
  • 178,314
  • 47
  • 389
  • 754
sg552
  • 1,365
  • 4
  • 26
  • 53

5 Answers5

2

Do something like :

 foreach($record_record as $sub_array){
        if (in_array('255', $sub_array, true)) {
           echo "'255' found with strict check\n";
        }
       else {
           echo "nope\n";
        } 
    }
greenLizard
  • 2,230
  • 5
  • 23
  • 30
2

You need to do something like this:

<?php

  function id_exists ($array, $id, $strict = FALSE) {
    // Loop outer array
    foreach ($array as $inner) {
      // Make sure id is set, and compare it to the search value
      if (isset($inner['id']) && (($strict) ? $inner['id'] === $id : $inner['id'] == $id)) {
        // We found it
        return TRUE;
      }
    }
    // We didn't find it
    return FALSE;
  }

  if (id_exists($record_record, 255, true)) {
    echo "'255' found with strict check\n";
  } else {
    echo "nope\n";
  } 
DaveRandom
  • 84,004
  • 11
  • 142
  • 168
  • thank you very much for the added comment. After searching related topic I assume PHP don't have this built in search function. One more thing, if I need to turn use strict I just need to change $strict to TRUE? Thanks again. – sg552 Feb 08 '12 at 15:25
  • All strict comparisons do is make sure that the types match as well as the value. So with strict comparisons, `255 !== '255'`. I have made the default value for strict `FALSE`, which means comparisons will be loose by default - if you want them to be strict by default, just change `FALSE` in the function definition to `TRUE`. – DaveRandom Feb 08 '12 at 15:34
1

You'll need a recursive function for that. From elusive:

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

    return false;
}

Or, if your array structure will never change, just write a simple loop:

function in_2dimensional_array($needle, $haystack, $strict = true){
   foreach ($haystack as $item) {
       if (in_array($needle, $haystack, true)) {
           return true;
       }
   }

   return false;
}
Community
  • 1
  • 1
Ben D
  • 13,146
  • 3
  • 43
  • 59
  • Direkt link: http://stackoverflow.com/a/4128377/701049 Edit: In this case you actually dont need a recursive search :) – tim Feb 08 '12 at 15:20
  • @Alexandrew Yeah, I was torn on whether this was a duplicate or not. The question is phrased in a different way, so for Google's sake it seems worth having both up. Should I make the citation more clear than just linking the original author's name? EDIT: the need for a recursive search is unclear from the question. The OP never specifies whether the search will always be on the ['id'] column. Granted, you could just write a loop (appended to answer) – Ben D Feb 08 '12 at 15:23
  • I think it's okay, I just thought for completelyness I provide a direct link to the answer (which you get by clicking `link` below an answer!) – tim Feb 08 '12 at 15:26
  • I just need to search the `[id]` column thats why I put `number` in my title – sg552 Feb 08 '12 at 15:30
0

Hacky solution. Someone else will post a nice one using array_map or something similar.

function in_nested_array($val, $arr)
{
    $matched = false;
    foreach ($arr AS $ar)
    {
        if (in_array($val, $ar, true)
        {
            $matched = true;
            break;
        }
    }
    return $matched;
}

if (in_nested_array(255, $record_record))
{
    // partay
}
Joe
  • 15,062
  • 4
  • 38
  • 77
0
<?php
foreach($record_record as $record) {
  $key = array_search('255', $record);
  if ($key) {
     echo "'255' found with strict check\n";
  }
}
?>
bizzr3
  • 1,685
  • 4
  • 23
  • 37