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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:55:39+00:00 2026-05-27T08:55:39+00:00

Given I have an array: $array = array( ‘a’ => array( ‘b’ => array(

  • 0

Given I have an array:

$array = array(
    'a' => array(
        'b' => array(
            'c' => 'hello',
        ),
    ),
    'd' => array(
        'e' => array(
            'f' => 'world',
        ),
    ),
);

I want to “flatten” it to a single dimension look-up of references, concatenating keys with a delimiter (in the case of this example, a forward slash /)

Performing a var_dump() on a successful output would yield: (note all the references)

array(6) {
  ["a"]=>
  &array(1) {
    ["b"]=>
    &array(1) {
      ["c"]=>
      &string(5) "hello"
    }
  }
  ["a/b"]=>
  &array(1) {
    ["c"]=>
    &string(5) "hello"
  }
  ["a/b/c"]=>
  &string(5) "hello"
  ["d"]=>
  &array(1) {
    ["e"]=>
    &array(1) {
      ["f"]=>
      &string(5) "world"
    }
  }
  ["d/e"]=>
  &array(1) {
    ["f"]=>
    &string(5) "world"
  }
  ["d/e/f"]=>
  &string(5) "world"
}
array(2) {
  ["a"]=>
  &array(1) {
    ["b"]=>
    &array(1) {
      ["c"]=>
      &string(5) "hello"
    }
  }
  ["d"]=>
  &array(1) {
    ["e"]=>
    &array(1) {
      ["f"]=>
      &string(5) "world"
    }
  }
}

As it stands, I’m using this:

function build_lookup(&$array, $keys = array()){
    $lookup = array();
    foreach($array as $key => &$value){
        $path = array_merge($keys, (Array) $key);
        $lookup[implode('/', $path)] = &$value;
        if(is_array($value)){
            $lookup = array_merge($lookup, build_lookup($value, $path));
        }
    }
    return $lookup;
}

However, I’m trying to improve on it by removing the element of recursion (switching to a stack/pop approach) The problem with doing so is reference preservation, as the typical recursion-to-non-recursion approach of:

$stack = $input;
while(!empty($stack)){
    $current = array_pop($stack);
    // do stuff and push to stack;
}

…fails with references.

I’ve seen a few similar questions/answers on SO, though none of which dealt appropriately with references (as it wasn’t the asker’s intent)

Is there a better (read faster) approach here?


The eventual solution (thanks @chris):

/**
 *
 * @return array
 */
public function get_lookup_array()
{
    $stack = $lookup = array();
    try
    {
        foreach($this->_array as $key => &$value)
        {
            $stack[$key] = &$value;
        }
        while(!empty($stack))
        {
            $path = key($stack);
            $lookup[$path] = &$stack[$path];
            if(is_array($lookup[$path]))
            {
                foreach($lookup[$path] as $key => &$value)
                {
                    $stack[$path . $this->_separator . $key] = &$value;
                }
            }
            unset($stack[$path]);
        }
    }
    catch(\Exception $exception)
    {
        return false;
    }
    return $lookup;
}
  • 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-27T08:55:40+00:00Added an answer on May 27, 2026 at 8:55 am
    header('content-type:text/plain');
    
    $arr = array(
        'a' => array(
            'b' => array(
                'c' => 'hello',
            ),
        ),
        'd' => array(
            'e' => array(
                'f' => 'world',
            ),
        ),
    );
    
    //prime the stack using our format
    $stack = array();
    foreach ($arr as $k => &$v) {
        $stack[] = array(
            'keyPath' => array($k),
            'node' => &$v
        );
    }
    
    $lookup = array();
    
    while ($stack) {
        $frame = array_pop($stack);
        $lookup[join('/', $frame['keyPath'])] = &$frame['node'];
        if (is_array($frame['node'])) {
            foreach ($frame['node'] as $key => &$node) {
                $keyPath = array_merge($frame['keyPath'], array($key));
                $stack[] = array(
                    'keyPath' => $keyPath,
                    'node' => &$node
                );
                $lookup[join('/', $keyPath)] = &$node;
            }
        }
    }
    
    
    var_dump($lookup);
    // check functionality
    $lookup['a'] = 0;
    $lookup['d/e/f'] = 1;
    var_dump($arr);
    

    Alternatively you could have done stuff like this to get a reference /w array_pop functionality

    end($stack);
    $k = key($stack);
    $v = &$stack[$k];
    unset($stack[$k]);
    

    That works because php arrays have elements ordered by key creation time. unset() deletes the key, and thus resets the creation time for that key.

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

Sidebar

Related Questions

Given an array of ids $galleries = array(1,2,5) I want to have a SQL
I have string that I want to chop to array of substrings of given
Given I have a HUGE array, and a value from it. I want to
If I have an array of points (x,y,z) and am given a single point
Hello frnds actually i have a array of arrays as given below:-` parrent array{
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
I have a Width and Height parametr. I have been given an array of
I have a string. I need to replace all instances of a given array
I have the array variable say $value, and it has the below given array
I have an array: $list = array('string1', 'string2', 'string3'); I want to get the

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.