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?

1
  • 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? Commented Feb 20, 2014 at 21:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I use this I get the following error: Warning: in_array() expects parameter 2 to be array, null given
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.
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('[email protected]', '[email protected]', 'test3@dfgfdgdfg');

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

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

?>

Comments

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

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.