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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:35:06+00:00 2026-06-14T23:35:06+00:00

Assume I have the following function function setArray(&$array, $key, $value) { $array[$key] = $value;

  • 0

Assume I have the following function

function setArray(&$array, $key, $value)
{
    $array[$key] = $value;     
}

In the above function, key is only at the first level, what if I want to set the key at the 2nd or 3rd levels, how to rewrite the function?

e.g.

 $array['foo']['bar'] = 'test';

I want to use the same function to set the array value

  • 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-14T23:35:07+00:00Added an answer on June 14, 2026 at 11:35 pm

    This one should work. Using this function you can set any array element in any depth by passing a single string containing the keys separated by .

    function setArray(&$array, $keys, $value) {
      $keys = explode(".", $keys);
      $current = &$array;
      foreach($keys as $key) {
        $current = &$current[$key];
      }
      $current = $value;
    }
    

    You can use this as follows:

    $array = Array();
    setArray($array, "key", Array('value' => 2));
    setArray($array, "key.test.value", 3);
    print_r($array);
    

    output:

    Array (
        [key] => Array
            (
                [value] => 2
                [test] => Array
                    (
                        [value] => 3
                    )
    
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following function: void myFunc(P& first, P& last) { std::cout <<
Assume I have the following style table, col1 col2 and col3 have same value
Assume I have a method/function with the following signature: foo($bar = 0) Inside foo
Let's assume I have the following multidimensional array (retrieved from MySQL or a service):
Assume I have the following function: // Precondition: foo is '0' or 'MAGIC_NUMBER_4711' //
Assume I have the following exemplary function: template <typename Fn> auto call(Fn fn) ->
Assume you have the following code: function name() { $(this).css('background', 'red'); } $('selector1').click(name); $('selector2').click(function
Possible Duplicate: Why is this function returning “undefined”? Let's assume I have the following
Assume I have the following example: Example One $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff();
Assume you have the following update: Update table set col1 = func(col2) where col1<>func(col2)

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.