0

I'm trying to find a part of a string in a multidimentional array.

foreach ($invitees as $invitee) {
  if (in_array($invitee, $result)){
    echo 'YES';
  } else {
    echo 'NO';
  }
}

the $invitees array has 2 elements: invitees array output

and $result is what I get from my Drupal database using db_select() result array output

What I'm trying to do is, if the first part from one of the emails in $invitees is in $result it should echo "YES". (the part before the "@" charather)

For example:

"test.email" is in $result, so => YES

"user.one" is not in $result, so => NO

How do i do this? How can I search for a part of a string in a multidimentional array?

Sidenote: I noticed that the array I get from Drupal ($result) has 2 "Objects" which contain a "String", and not arrays like I would expect.

For example:

$test = array('red', 'green', array('apple', 'banana'));

Difference between $result and $test: enter image description here Does this have any effect on how I should search for my string?

Jrn
  • 1,185
  • 1
  • 13
  • 27
  • Can you do some output from PHP (`var_dump()`) instead of screenshots? What is your input data and what output are you getting? How is it different from what you expect? – miken32 Feb 20 '14 at 21:38

3 Answers3

1

Because $result is an array of objects, you'll need to use a method to access the value and compare it. So, for instance you could do:

//1: create new array $results from array of objects in $result
foreach ($result as $r) {
    $results[] = get_object_vars($r);
}

//2: expanded, recursive in_array function for use with multidimensional arrays
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;
}

//3: check each element of the $invitees array
foreach ($invitees as $invitee) {
    echo in_array_r($invitee, $results) ? "Yes" : "No";
}

Also, for some illumination, check out this answer.

Community
  • 1
  • 1
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • If I use this I get the following error: Warning: in_array() expects parameter 2 to be array, null given – Jrn Feb 21 '14 at 08:38
  • I accepted your answer because you pointed my in the direction of recursive functions. I posted the code I ended up using below in my own answer. – Jrn Feb 21 '14 at 16:51
0

You can search through the array using preg_grep, and use a wildcard for anything before and after it. If it returns a value (or values), use key to get the index of the first one. Then do a check if its greater than or equal to 0, which means it found a match :)

<?php

$array = array('test1@gdfgfdg.com', 'test2@dgdgfdg.com', 'test3@dfgfdgdfg');

$invitee = 'test2';
$result = key(preg_grep('/^.*'.$invitee.'.*/', $array));

if ($result >= 0) {
    echo 'YES';
} else {
    echo 'NO';
}

?>
weiver
  • 226
  • 2
  • 3
0

Accepted larsAnders's answer since he pointed me in to direction of recursive functions. This is what I ended up using (bases on his answer):

function Array_search($array, $string) {
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      Array_search($array[$key], $string);
    } else {
      if ($value->data == $string) {
        return TRUE;
      }
    }
  }
  return FALSE;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jrn
  • 1,185
  • 1
  • 13
  • 27