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 1012759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:59:40+00:00 2026-05-16T09:59:40+00:00

I have one array that contains some settings that looks like basically like this:

  • 0

I have one array that contains some settings that looks like basically like this:

$defaults = array(
   'variable' => 'value', 
   'thearray' => array(
                  'foo' => 'bar'
                  'myvar' => array('morevars' => 'morevalues');
                  );
);

On another file, i get a string with the first level key and it’s childs to check if there is a value attached to it. Using the array above, i’d get something like this:

$option = "thearray['myvar']['morevars']";

I need to keep this string with a similar format to the above because I also need to pass it to another function that saves to a database and having it in an array’s format comes in handy.

My question is, having the array and the string above, how can i check for both, existance and value of the given key inside the array? array_key_exists doesn’t seem to work below the first level.

  • 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-05-16T09:59:40+00:00Added an answer on May 16, 2026 at 9:59 am

    You could use a simple function to parse your key-string and examine the array like:

    function array_deep_exists($array, $key)
    {
        $keys = preg_split("/'\\]|\\['/", $key, NULL, PREG_SPLIT_NO_EMPTY);
        foreach ($keys as $key)
        {
            if ( ! array_key_exists($key, $array))
            {
                return false;
            }
            $array = $array[$key];
        }
    
        return true;
    }
    
    // Example usage
    $defaults = array(
        'variable' => 'value', 
        'thearray' => array(
            'foo' => 'bar',
            'myvar' => array('morevars' => 'morevalues')
        )
    );
    $option = "thearray['myvar']['morevars']";
    $exists = array_deep_exists($defaults, $option);
    var_dump($exists);  // bool(true)
    

    Finally, to get the value (if it exists) return $array where the above returns true.

    Note that if your array might contain false, then when returning the value you’ll have to be careful to differentiate no-matching-value from a successful false value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an $input array, that contains something like this : array 0
I have one array that is filled by mysql_query. Values that are in this
I have one array. I want that array to retain its value between function
I have a for loop that loops over one array... for i=1:length(myArray) In this
I currently have an Array that contains some URL's to images. @images = [
I have a file that contains parameters using this syntax RANGE {<value> | <value>-<value>}
I have two arrays of data that I'm trying to amalgamate. One contains actual
I have 2 arrays that I would like to render in a template, one
I have some Javascript code that creates 2 arrays: One for Product Category and
I am asking for your ideas regarding this problem: I have one array A,

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.