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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:46:43+00:00 2026-05-22T14:46:43+00:00

Possible Duplicate: Check if a “run-time” multidimensional array key exists Hi, I have a

  • 0

Possible Duplicate:
Check if a “run-time” multidimensional array key exists

Hi,

I have a multidimensional array. I need a function that checks if a specified key exists and if not set the value.

Let’s take this array

$config['lib']['template']['engine'] = false;

A function should not update the value to true when i call with:

checkAndSetKey('lib template engine',true);
//> Checks if isset $config['lib']['template']['engine'] and if not isset $config['lib']['template']['engine'] = true;

Note that my array isn’t only 3 dimensional. It should be able to check and set even with only 1 dimension:

checkAndSetKey('genericSetting',true);
//> In this considering there isn't any $c['genericSetting'] the function set the key to true;

At the moment I am using an awful eval code, I would like to hear suggest 🙂

To dynamically check if the key exists it could be used this code:

$array = $config;
$keys=explode(' ',$argument1);

foreach($keys as $v) { 

    if (!array_key_exists($v,$array)) {
        //> [todo!] the current key doens't exist now we should set the value
    }

    $array = &$array[$v];
}
  • 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-22T14:46:43+00:00Added an answer on May 22, 2026 at 2:46 pm

    LOL actually this was just one line of code added there =/
    For a moment the references confused me a bit.

    Here the code :

    function _c($key,$value) {
        global $c;
    
        $array = &$c;
        $keys = explode(' ',$key);
    
        $setValue = false;
        for($i=0;$i<count($keys);$i++) { 
            $v = $keys[$i];
    
            //> If setValue is already = true we don't need to check it again
            if (!$setValue && !array_key_exists($v,$array))
                $setValue = true;
    
            $array = &$array[$v];
        }
    
        if ($setValue)
            $array = $value;
    }
    
    //> Usage _c('lib template engine',true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: “main” function in Lua? In Python, you can check if a script
Possible Duplicate: Is there an “exists” function for jQuery Is there a better way
Possible Duplicate: How to check if an array value exists I have a problem
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: MySQL check if a table exists without throwing an exception I have
Possible Duplicate: Check status of services that run in a remote computer using C#
Possible Duplicates: Is there an “exists” function for jQuery jQuery determining if element exists
Possible Duplicate: Check if $_POST exists I'm trying to run something if and only
Possible Duplicate: How to implement “select all” check box in HTML? Select all and
Possible Duplicate: Application Crashes With “Internal Error In The .NET Runtime” I have 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.