0

I have an array like this, that could have any number of "domain" arrays. I've removed unnecessary keys in this example.

Array
(
    [result] => success
    [clientid] => 7
    [numreturned] => 2
    [domains] => Array
        (
            [domain] => Array
                (
                    [0] => Array
                        (
                            [domainname] => example.net
                        )

                    [1] => Array
                        (
                            [domainname] => example.com
                        )

                )

        )

)

I need to figure out how to check this array to see if it contains a domain name.

I'm trying to do something like this:

if(arrayContains("example.com")){
$success = true;
}

I have tried a number of solutions I found on SO, but they don't appear to be working. One example I found used array_key_exists, which is kind of the opposite of what I need.

Any suggestions on how to do this?

4
  • 2
    Have you looked at array_search()? - php.net/manual/fr/function.array-search.php#91365 Commented Mar 25, 2015 at 10:50
  • I tried the code you linked to in that comment, and it worked. If you want to post this as an answer I'll accept it, as you were the first to answer, and your solution worked. Commented Mar 25, 2015 at 11:08
  • 1
    Glad it worked. I'll post an answer as soon as I get back to my PC. Commented Mar 25, 2015 at 11:11
  • @SherwinFlight If by any chance you used the code I posted. I wanted to let you know I've edited my answer. The previous answer was buggy. Commented Mar 25, 2015 at 11:25

3 Answers 3

1

Use this function to help you out:

<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}
?>

This was found in one of the comments in the PHP doc about array_search().

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

Comments

1
$array = array(
    "result" => "success",
    "clientid" => 7,
    "numreturned" => 2,
    "domains" => array(
            "domain" => array(
                    0 => array(
                            "domainname" => "somedomain.com",
                            3 => array(
                                "domainname" => "searchdomanin.com",

                                ),
                        ),

                    1 => array(
                            "domainname" => "extwam",
                        ),

                )

        )

);

$succes = FALSE;
$search = 'searchdomanin.com';

array_walk_recursive($array, function($key, $value) use(&$success, &$search){ 
    if($key === $search){
       $success = TRUE;
    }
},
[&$key ,&$val]);

if($success){
    echo 'FOUND';
}

Works with whatever dimension array you have.

Comments

0

Try something like this:

$domains = $arr['domains'];
foreach($domains AS $domain)
{
   foreach($domain AS $internal_arr)
   {
      if($internal_arr['domainname'] == 'example.net')
      {
        $success = true;
      }
   }
}

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.