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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:11:51+00:00 2026-06-02T03:11:51+00:00

EDIT I know I’m a new poster here, but I’m not new to StackOverflow.

  • 0

EDIT

I know I’m a new poster here, but I’m not new to StackOverflow. I’ve never seen people make up completely new solutions to problems as I’m seeing below. I provided the input, the output, and the expected output. The accepted answer will work within these guidelines.


I have am trying to write an array writing function that works as described below

// existing array
$config = array("preserve" => "please");

function set($key, $value){
    global $config;
    if(count($keys = explode(':', $key)) > 1){
        $key = array_shift($keys);
        while($k = array_pop($keys)){
            $value = array($k => $value);
        }
    }
    return $config[$key] = $value;   
}

// set
set("foo", "bar");
set("pokemon:trainer", "ashk");
set("pokemon:favorite", "diglett"); // overwrites pokemon:trainer :(

print_r($config);

Output

Array
(
    [preserve] => please
    [foo] => bar
    [pokemon] => Array
        (
            [favorite] => diglett
        )

)

Oh no! Who’s the pokemon trainer? Ash Ketchum has been forgotten!


I understand why it’s breaking, but I can’t find out how I should write this function. I sense the need for recursion, but I’m not sure how to implement it :\

Output should be

Array
(
    [preserve] => please
    [foo] => bar
    [pokemon] => Array
        (
            [trainer] => ashk
            [favorite] => diglett
        )

)
  • 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-02T03:11:52+00:00Added an answer on June 2, 2026 at 3:11 am

    Now that I have over 100 rep, I can finally post this as a real answer and mark it as accepted.

    This is quite a neat little snippet 🙂

    // existing array
    $config = array("preserve" => "please");
    
    function set($key, $value){
        global $config;
        if(count($keys = explode(':', $key)) > 1){
            $key = array_shift($keys);
            while($k = array_pop($keys)){
                $value = array($k => $value);
            }
            // *** fixed here ***
            // merge new data with possible existing data
            if(array_key_exists($key, $config)){
                $value = array_merge_recursive($config[$key], $value);
            }
        }
        return $config[$key] = $value;   
    }
    
    // set
    set("foo", "bar");
    set("pokemon:trainer", "ashk");
    set("pokemon:favorite", "diglett"); // no longer overwrites pokemon:trainer :)
    
    print_r($config);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I've found what's causing the issue, but I don't know why and I
I hope this question has not been asked yet, but I want to know
EDIT: I know it is a little strange design, and so it is difficult
does anyone know how you edit/maintain regions in magento - specifically adding ones for
Does anyone know how can I edit a subitem on a listView? I've tried
Does anyone know how to allow users to edit another user´s email in a
Does anyone know how to enable or disable programmatically the Quick Edit Mode in
Most of you probably know the following problem: You edit an HTML, view the
I would like to know how I could edit mp3 files on the server
Is there a way to know if TAdoQuery in Edit state was changed? Assuming

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.