Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
Have you looked at array_search()? - php.net/manual/fr/function.array-search.php#91365 –  D4V1D 19 hours ago
    
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. –  Sherwin Flight 19 hours ago
1  
Glad it worked. I'll post an answer as soon as I get back to my PC. –  D4V1D 19 hours ago
    
@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. –  Andrei P. 19 hours ago

4 Answers 4

up vote 1 down vote accepted

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().

share|improve this answer
$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.

share|improve this answer

Try something like this:

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

More recurrent attempt:

For data

    $array = [
        'result' => 'success',
        'domains' => [
            'domain' => [
                ['title' => 'example domains'],
                ['domainname' => 'example.net'],
                ['domainname' => 'example.com']
            ]
        ]
    ];

method declaration:

    function domainnameFilter($array) {
        $result = [];
        foreach($array as $key => $value) {
            if($key === 'domainname') {
                $result[] = $value;
            } elseif (is_array($value)) {
                $sub = domainnameFilter($value);
                if($sub) {
                    $result = array_merge($result, $sub);
                }
            }
        }
        return $result;
    }

test results

    $domains = domainnameFilter($array);
    var_dump($domains);

and result:

array(2) {
  [0] =>
  string(11) "example.net"
  [1] =>
  string(11) "example.com"
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.