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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:31:56+00:00 2026-06-05T04:31:56+00:00

So my example inputs are $example_1 = Array ( 0 => Array ( ‘category’

  • 0

So my example inputs are

$example_1 = Array (
    0 => Array (
        'category'     => 'body',
        'sub-category' => 'intro',
        'id'           => 'header',
        'copy'         => 'Hello',
    ),
    1 => Array (
        'category'     => 'body',
        'sub-category' => 'intro',
        'id'           => 'footer',
        'copy'         => 'Bye',
    ),
);

$example_2 = Array (
    0 => Array (
        'category'     => 'body',
        'sub-category' => 'intro',
        'sub-sub-category' => 'header',
        'sub-sub-child-category' => 'left',
        'id'           => 'title',
        'copy'         => 'Hello',
    ),
    1 => Array (
        'category'     => 'body',
        'sub-category' => 'intro',
        'sub-sub-category' => 'footer',
        'sub-sub-child-category' => 'right',
        'id'           => 'title',
        'copy'         => 'Bye',
    ),
);

I want to transform it into

$example_output_1 = Array (
    'body' => Array (
        'intro' => Array (
            'header' => Array (
                'title' => 'Hello',
            ),
            'footer' => Array (
                'title' => 'Bye',
            ),
        ),
    ),
);

$example_output_2 = Array (
    'body' => Array (
        'intro' => Array (
            'header' => Array ( 
                'left' => Array (
                    'title' => 'Hello',
                ),
            ),
            'footer' => Array (
                'right' => Array (
                    'title' => 'Bye',
                )
            ),
        ),
    ),
);

Note the depth of the array is dynamic (it is not set – only by the time it hits ‘copy’ does it indicate the depth of the array).

I am having problems trying to get the recursion correctly. The basic but very rough algorithm I had was to
– Loop through the Row
– Loop through the contents of the Row
– When the index is “copy” then the final value is current value.
– Then build the array

I managed to get it to process for ONLY one row of the array but it was very messy and kinda patchy, so I got a feeling I really need to start from scratch.
Updated: Attached embarrassing Code as requested (don’t scream! ;p)

function buildArray($row, $start = true) {

    if ($start) {
        $result = array();
    }

    if ( ! is_array($row) ) {
        return $row;
    }

    // Get the first element of the array include its index
    $cellValue = null;
    $colId = null;
    foreach($row AS $index => $value) {
        $cellValue = $value;
        $colId = $index;
        break; 
    }

    // Reduce the array by one
    $tempRow = $row;
    $temp = array_shift($tempRow);

    if ($colId == 'copy') {
        $result[$cell] = buildArray($cellValue, $locale, false);
    } else {
      $result[$cell] = buildArray($tempRow, $locale, false);
    }

    return $result;
} 

Any help will be greatly appreciated.

  • 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-05T04:31:59+00:00Added an answer on June 5, 2026 at 4:31 am

    This can be solved iteratively, because the recursion would only happen at the tail end of your function. The following code is the simplification. It builds a new layered array while it iterates over the old.

    After transforming each each entry it gets merged using array_merge_recursive.

    function transform($a)
    {
        // create new array and keep a reference to it
        $b = array(); $cur = &$b;
        foreach ($a as $key => $value) {
            if ('id' === $key) {
                // we're done, add value to the array built so far using id and copy
                $cur[$value] = $a['copy'];
                break;
            } else {
                // create one more level
                $cur[$value] = array();
                // and update the reference
                $cur = &$cur[$value];
            }
        }
        // all done
        return $b;
    }
    
    // $example_2 is your multi-dimensional array
    $merged = call_user_func_array('array_merge_recursive', 
        array_map('transform', $example_2)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a script which take hh:mm:ss inputs for example vid_cut.py -t 00:00:30 00:10:00
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
How to copy xml document skipping some top level nodes. For example: Input: <root>
I'm trying to use an array to hold inputs for a survey that will
I have 2 select inputs and I filling them with an array of years.
I want to replace i# (look that the example inputs/outputs) with the value of
Example input: SERVER_NAME=server1 PROFILE_NAME=profile1 ... Example output: SERVER_NAME=server3 PROFILE_NAME=profile3 ... This file will use
For example : input = 360 seconds output = 6 minutes input = 86400
For example Input <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Desired output <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Here's a sample
How to split a string in SQL Server. Example: Input string: stack over flow

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.