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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:58:34+00:00 2026-05-16T07:58:34+00:00

I have a messy tree multidimensional array that I want to do the following

  • 0

I have a messy tree multidimensional array that I want to do the following to:

Extract each array, no matter how far nested down to put it into a single “holder array”, so this (just a basic example as it would be much more complex than this as far as the nesting)

$this = array[0]=> (array[1]=>('a','b'),
                    array[2]=>(array[3]=>('c','d')));

would become something like this, it doesn’t matter if it changes the index for each array, just so that they are still in an array, but “flat” so the only nesting is within the one main holder array

$would_become = array[holder]=>(array[1]=>('a','b'),
                                array[2]=>(),
                                array[3]=>('c','d'));

The overall reasoning behind this is that I have a bunch of nested arrays that have a common key, such as [‘filepath’] and I want to be able to do something like below (would need to make it go through each array in the holder array obviously, but this shows the basic idea behind why i need this.

foreach ($holder_array as $holder_array) {

// as an example:
echo $holder_array['Path']
}
  • 1 1 Answer
  • 2 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-16T07:58:34+00:00Added an answer on May 16, 2026 at 7:58 am
    <?php
    
    $in = array(
            array('a','b'),
            array(
                array('c','d')
            )
    );
    
    function flatten($in) {
        $result = array();
    
        foreach ($in as $item) {
            if (is_array($item)) {
                $result[] = array_filter($item, 'notArray');
                $result = array_merge($result, flatten($item));
            } 
        }
    
        return $result;
    }
    
    
    function notArray($in) {
        return ! is_array($in);
    }
    
    
    $result = flatten($in);
    print_r($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We (my colleagues) have a messy 12 y.o. mature app that is GUI-based, and
I have inherited a somewhat messy query that I am working on refactoring to
I have the following messy HTML table which is being used to present a
I have a website (http://nottingham.subverb.net) that is mainly made up of messy procedural code
I have built a family tree program which thus far allows you to add
I want to create a tree that it's child nodes contain specific flex components
Let's say that we have an array with n elements (n > 0). We
I have a set of tables that's actually a stubby tree. At the top,
I have a git repository that is just a directory tree (/myprogram/src/com/mycompany/test) with some
So basically I have an ordered binary tree, and I want to be able

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.