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

The Archive Base Latest Questions

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

$foo = array( ‘1’ => ‘2’, ‘3’ => array( ‘4’ => ‘5’ ), ‘6’

  • 0
$foo = array(
    '1' => '2',
    '3' => array(
        '4' => '5'
    ),
    '6' => array(
        '7' => '8',
        '9' => '10',
        '11' => array(
            '12' => '13',
            '14' => '15'
        )
    )
);

$bar = array(
    '1',
    '6' => array(
        '7',
        '11' => array(
            '12'
        )
    )
);

Foo is an array i have to edit, Bar the edits i need to do.

I have to create another element in Foo array containing the elements pointed in Bar, and delete the originals from Foo.

So, with the array, the final array should be:

Array(
    '3' => array(
        '4' => '5'
    ),
    '6' => array(
        '9' => '10',
        '11' => array(
            '14' => '15'
        )
    ),
    'merged' => array(
        '1' => '2',
        '6' => array(
            '7' => '8',
            '11' => array(
                '12' => '13'
            )
        )
    )
)

I’ve build this recursive function, but works only for the first level of the array:

foreach($bar AS $key => $value){
    if(is_array($value)){
        s($foo, $key, $value);
    }else{
        $foo['merged'][$value] = $foo[$value];
        unset($foo[$value]);
    }
}


function s(&$form, $key, $value){
    if(is_array($value)){
        foreach($value AS $k => $v){
            s($form, $k, $v);
        }
    }else{
        $form['merged'][$value] = $form[$value];
        unset($foo[$value]);
    }
}

Any ideas?

  • 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-17T02:25:04+00:00Added an answer on May 17, 2026 at 2:25 am

    The biggest issue with your script at the moment is that you assume an element without a key is a stand-alone construct. The array $bar actually looks like this to PHP:

    $bar = array(
        '0' => '1',
        '6' => array(
            '0' => '7',
            '11' => array(
                '0' => '12'
            )
        )
    )
    

    Realizing this, when you see the key ‘0’ in $bar it’s obvious we should look at the value and move that key=>value pair to $foo[‘merged’], but it becomes more complicated when you see ‘6’. It’s even more complicated when you realize you can’t just nest foreach() loops since there could be potentially infinite levels to this array.

    The trick to dealing with an arbitrary number of levels in any abstract data type is a recursive function with a static counter (for keeping track of level). This way we can keep going deeper into $bar, but we back out to exactly where we left out when we’re done. If we make the counter an arary we can keep track of how we got to where we are. This way we can find the element within $foo later.

    /* recursive_foobar is the only function you call */
    
    function recursive_foobar(&$foo, $bar, &$merged){
        static $counter;
        if(is_empty($counter)){ // initialize counter the first time
            $counter = array();
        }
        foreach($bar as $key => $element){
            if(is_array($element)){
                $counter[] = $key;
                recursive_foobar($foo, $element, $merged[$key]);
            } else {
                $old_value = recursive_get($foo, array_push($counter, $element));
                recursive_remove($foo, array_push($counter, $element));
                array_merge($merged, $old_value);
            }
        }
        return $merged;
    }
    
    /* recursive_get returns a multi-level array containing the requested element at the lowest level */
    
    function recursive_get($haystack, $key){
        static $return;
        if(count($key) > 1){
            $return[] = array(recursive_get($haystack[$key[0]], array_shift($key)));
        } else {
            $return[] = $haystack[$key[0]];
        }
        return $return;
    }
    
    /* recursive_remove will remove the requested element, leaving all containers untouched */
    
    function recursive_remove(&$array, $key){
        if(count($key) > 1){
            recursive_remove($array[$key[0]], array_shift($key));
        } else {
            remove($array[$key[0]]) ???
        }
    }
    
    $foo['merged'] = array();
    recursive_foobar($foo, $bar, $foo['merged']);
    

    This is kind of sloppy, but what you requested involved some pretty high-level constructs and some complicated logic. There may be a few PHP functions I haven’t memorized that could shave down a little bit of the code, but you’re talking about removing arbitrary elements from arrays of arbitrary length with arbitrary depths and arbitrary number of times…

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

Sidebar

Related Questions

Assume I have an array: $elements = array('foo', 'bar', 'tar', 'dar'); Then I want
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
Consider I have two arrays: $friends = Array('foo', 'bar', 'alpha'); $attendees = Array('foo', 'bar');
I have the array: example = ['foo', 'bar', 'quux'] I want to iterate over
I have a simple associative array: $ar = array( 1=>'foo', 2=>'bar', 5=>'foobar', 8=>'barfoo' )
Say I have an array: $myArray = array(foo, bar); What is a good way
I have an array with foo.bar.baz as key names in the array. Is there
I have an array of n elements, of the form: array ( array (FOO,
I've an array like $ignore_post = array(foo, bar); and I need to check if
I have a function returning an array: function foo(){ return array('foo'=>1,'bar'=>2); } Can I

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.