0

I am currently using this method to find arrays by keys + values:

public static function getCountryDataByVal($array, $key, $value, &$results = array()) {
    if (!is_array($array)) {
        return;
    }

    if (isset($array[$key]) && $array[$key] == $value) {
        $results[] = $array;
    }

    foreach ($array as $subarray) {
        self::getCountryDataByVal($subarray, $key, $value, $results);
    }

    return $results;
}

This works fine so far, but there's a problem and i have no idea how to solve it. Let's assume, that i have this array structure:

array(8) {
    ["alpha2"]=>
        string(2) "ad"
    ["alpha3"]=>
        string(3) "and"
    ["numeric3"]=>
        string(3) "020"
    ["callingCodes"]=>
        array(1) {
            [0]=>
                string(3) "376"
        }
    ["tlds"]=>
        array(1) {
            [0]=>
                string(2) "ad"
        }
    ["currencies"]=>
        array(1) {
            [0]=>
                string(3) "eur"
        }
    ["longitude"]=>
        string(4) "42.5"
    ["latitude"]=>
        string(3) "1.5"
}

I can use the method above to find this array by alpha2 = ad for example. But what i need is the possibility to search for eur IN currencies for example.

Using this does not work:

getCountryDataByVal($array, 'currencies', 'eur');

because eur is not the value of the key currencies, but it's the value of a subkey.

Any idea, how i have to extend the method above to achieve this?

4
  • getCountryDataByVal($array['currencies'], 'eur'); ? :p Commented Mar 1, 2015 at 19:44
  • php.net/manual/en/function.array-search.php#91365 seems like a nice recursive array_search function Commented Mar 1, 2015 at 19:46
  • getCountryDataByVal($array['currencies'], 'eur'); wont work and the function you've linked doesn't fit my needs. Commented Mar 1, 2015 at 19:59
  • array_search('eur',$array['currencies']) ? Commented Mar 1, 2015 at 20:02

1 Answer 1

1

try that function:

 public static function getCountryDataByVal($array, $key, $value, &$results = array()) {
    if (!is_array($array)) {
        return;
    }

    if ((isset($array[$key]) && $array[$key] == $value )|| 
       (isset($array[$key]) &&  is_array($array[$key]) && array_search($value, $array[$key]) !== FALSE ) ) 
    {
        $results[] = $array;
    }

    foreach ($array as $subarray) {
        self::getCountryDataByVal($subarray, $key, $value, $results);
    }

    return $results;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Undefined index: currencies
could you try again, forget to add brackets for grouping in if-statement
Tried that already (if (isset($array[$key]) && ($array[$key] == $value || (is_array($array[$key]) && array_search($value, $array[$key]))))). However, i just get an empty result.
ok one more edit in the if-statement because array_search returns index (may be 0, so interpreted as false) or FALSE

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.