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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:22:03+00:00 2026-05-25T15:22:03+00:00

$first = array(a, b => array(c, d => array(e, f)), g, h => array(f));

  • 0
$first = array("a", "b" => array("c", "d" => array("e", "f")), "g", "h" => array("f"));
$second = array("b", "d", "f");
$string = "foobar";

Given the above code, how can I set a value in $first at the indexes defined in $second to the contents of $string? Meaning, for this example, it should be $first["b"]["d"]["f"] = $string;, but the contents of $second and $first can be of any length. $second will always be one dimensional however. Here’s what I had tried, which didn’t seem to work as planned:

$key = "";
$ptr = $first;
for($i = 0; $i < count($second); $i++)
{
    $ptr &= $ptr[$second[$i]];
    $key = key($ptr);
}
$first[$key] = $string;

This will do $first["f"] = $string; instead of the proper multidimensional indexes. I had thought that using key would find the location within the array including the levels it had already moved down.

How can I access the proper keys dynamically? I could manage this if the number of dimensions were static.

EDIT: Also, I’d like a way to do this which does not use eval.

  • 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-25T15:22:04+00:00Added an answer on May 25, 2026 at 3:22 pm

    It’s a bit more complicated than that. You have to initialize every level if it does not exist yet. But your actual problems are:

    • The array you want to add the value to is in $ptr, not in $first.
    • $x &= $y is shorthand for $x = $x & $y (bitwise AND). What you want is x = &$y (assign by reference).

    This should do it:

    function assign(&$array, $keys, $value) {
        $last_key = array_pop($keys);
        $tmp = &$array;
        foreach($keys as $key) {
            if(!isset($tmp[$key]) || !is_array($tmp[$key])) {
                $tmp[$key] = array();
            }
            $tmp = &$tmp[$key];
        }
        $tmp[$last_key] = $value;
        unset($tmp);
    }
    

    Usage:

    assign($first, $second, $string);
    

    DEMO

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

Sidebar

Related Questions

The first array is called $related_docs and the second on is $all_docs. I'm trying
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have a php file somejson.php that echos a json encoded array {jsonone:first json,jsontwo:second
example some array {2,8,9,10,21,32,1,6,3...} first child take (data size / 2) and sort second
Consider a string array shaped like this: string[] someName = new string[] { First,
How can I take the first row of a cell array that contains doubles
$array = array( 'first element', 'second element', 'third element', ); echo $array['1']; echo $array[1];
There are 2 functions involved. Search array items for a given string unset() array
I know I can declare an array of string in XAML like this: <x:Array
Consider the following Scala code: object MainObject { def main(args: Array[String]) { import Integer.{

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.