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

  • Home
  • SEARCH
  • 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 8421711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:13:20+00:00 2026-06-10T03:13:20+00:00

Okay this might be confusing. I’m trying to make a config class for myself

  • 0

Okay this might be confusing. I’m trying to make a config class for myself and I want its usage to be something like this:

$this->config->add('world', 'hello');
// This will create array('hello' => 'world')

Now my question is, if I want to add my value into a multi-dimensional array that doesn’t exists but would like to create it using something like this:

$this->config->add('my value', 'hello => world');
// This should create array('hello' => array('world' => 'my value'))

$this->config->add('my value', 'hello => world => again');
// This should create array('hello' => array('world' => array('again' =>'my value')))

I’m having trouble converting 'hello => world' into an array with the value set to the last array element.

This is what I have so far.

    public function add($val, $key = null)
    {
        if (is_null($key))
        {
            $this->_config = $val;

        } else {

            $key = explode('=>', str_replace(' ', '', $key));

            if (count($key) > 1)
            {
                // ?

            } else {

                if (array_key_exists($key[0], $this->_config))
                {
                    $this->_config[$key[0]][] = $val;

                } else {

                    $this->_config[$key[0] = $val;
                }
            }
        }
    }
  • 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-10T03:13:21+00:00Added an answer on June 10, 2026 at 3:13 am
    public function add($val, $key = null)
    {
        $config=array();
        if (is_null($key))
        {
            $this->config = $val;
    
        } else {
            $key = explode('=>', str_replace(' ', '', $key));
            $current=&$this->config;
            $c=count($key);
            $i=0;
            foreach($key as $k)
            {
                $i++;
                if($i==$c)
                {
                    $current[$k]=$val;
                }
                else
                {
                    if(!isset($current[$k]) || !is_array($current[$k])) $current[$k]=array();
                    $current=&$current[$k];
                }
            }
        }
    }
    

    Here’s a recursive version, It will be more resource-expensive:

    public function add($val, $key = null)
    {
        $this->config=array();
        if (is_null($key))
        {
            $config = $val;
        } else {
            $key = array_reverse(explode('=>', str_replace(' ', '', $key)));
            self::addHelper($val,$key,$config);
        }
    }
    private static function addHelper(&$val,&$keys,&$current)
    {
        $k=array_pop($keys);
        if(count($keys)>0)
        {
            if(!isset($current[$k]) || !is_array($current[$k])) $current[$k]=array();
            addHelper(&$val,$keys,&$current[$k]);
        }
        else $current[$k]=$val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this might be confusing. What I'm trying to do is use an enumerator
Okay, the post title might be a little confusing. I have this code: class
okay this might be a pretty lame and basic question but its stuck in
Okay so the title might sound a bit confusing, but here's what I want
Okay, this might seem like a weird question, but bear with me. So I
This might be a n00bish question, but whatever. Is okay to use exceptions for
Okay, this question comes through a friend so it might be lost in translation...
Okay, I might confuse some people who might be able to answer this question,
Okay this might sound simple but there's a catch. I'll try and explain the
Okay, this might be a very silly beginner question, but: I've got an ClassA,

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.