Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8905403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:08+00:00 2026-06-15T02:23:08+00:00

So I wrote a function that searches two levels deep into an array for

  • 0

So I wrote a function that searches two levels deep into an array for a key and subkey pair. Basically it is looking for key['subkey'] and if it finds it, returns key['subkey'].

What I am looking for is a way to do this in a manner that is truly recursive and searches as many levels deep as needed until it either reaches the end of the array and is forced to return false, or finds the value and returns it.

I am completely newbish at PHP, and I have googled for hours with no result. Any pointers in the right direction would be appreciated.

This is the function:

function searchArray($array, $key, $subkey) {
    foreach ($array as $item){
        if (is_array($item) && isset($item[$key]) && isset($item[$key][$subkey])){
            return $item[$key][$subkey];
        } else {
            foreach ($item as $subitem){
                if (is_array($subitem) && isset($subitem[$key]) && isset($subitem[$key][$subkey])){
                    return $subitem[$key][$subkey]; 
                }
            }
        }
    }
}

Sample Array:

"locales" => array(
    "America" => array(
        "locations" => array(
            "us" => array(
                "title" => "United States",
                "lang" => "en_US",
            ),
            "gl" => array(
            )
        )
    ),
    "Europe" => array(
        "locations" => array(
            "at" => array(
                "title" => "Österreich",
                "lang" => "de_DE",
            ),
            "fr" => array(
                "title" => "France",
                "lang" => "fr_FR",
            ),
            "de" => array(
                "title" => "Deutschland",
                "lang" => "de_DE",
            ),
            "it" => array(
                "title" => "Italy",
                "lang" => "it_IT",
            ),
            "uk" => array(
                "title" => "United Kingdom",
                "lang" => "en_GB",
            )
        )   
    ),
    "Africa" => array(
        "locations" => array(
            "za" => array(
                "title" => "Südafrika",
                "lang" => "en_ZA",
            )
        )
    ),
    "Asia & Pacific" => array(
        "locations" => array(
            "au" => array(
                "title" => "Australia",
                "lang" => "en_AU",
            ),
            "cn" => array(
                "title" => "中国",
                "lang" => "zh_CN",
            ),
            "hk" => array(
                "title" => "香港",
                "lang" => "zh_CN",
            ),
            "jp" => array(
                "title" => "日本",
                "lang" => "jp_JP",
            ),
            "kr" => array(
                "title" => "한국",
                "lang" => "ko_KR",
            )
        )
    )
)

calling searchArray($siteOptions['locales'], 'us', 'lang') should return 'en_US'.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:23:09+00:00Added an answer on June 15, 2026 at 2:23 am
    function walk($array, $key, $option) {
        if( !is_array( $array)) {
            return false;
        }
        foreach ($array as $k => $v) {
            if($k == $key && is_array($v) && isset($v[$option])){
                return $v[$option];
            }
            $data = walk($v, $key, $option);
            if($data != false){
                return $data;        
            }
        }
    
        return false;
    }
    
    echo walk($array, 'jp', 'lang');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a function that searches for a char in an array, and if
I wrote a function that scans a tab delimited file of baseball stats. public
So I wrote this function that is given possible numbers, and it has to
i wrote a custom jquery function that accept a callback but not being able
I wrote a simple function, that makes emacs add matching quotes (so when I
I've got a function I wrote quite some time ago that works fine, but
I have a C++ DLL that I wrote that has a single exposed function,
Is there a way to write the PHP file_exists function so that it searches
So i am trying to make a function that searches trough an xml file,
I wrote this extension that basically changes the look of a website using CSS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.