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

  • Home
  • SEARCH
  • 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 8594801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:16:56+00:00 2026-06-12T00:16:56+00:00

I have an ordered array so that each ref key is guaranteed to exist

  • 0

I have an ordered array so that each ref key is guaranteed to exist among the former elements in the same array. However, the reference might be inside another array in a former element. So I’m using a recursive function to find the appropriate place, but for some reason I can not delete the former element.

What I can do: Copy necessary elements to where they belong
What I can not do: Move these elements across arrays

Here’s a pseudo output of the original array.

Array
(
    [0] => Array
        (
            [id] => 1
            [ref] => 
            [arr] => Array ()
        )
    [1] => Array
        (
            [id] => 2
            [ref] => 1
            [arr] => Array ()
        )
    [2] => Array
        (
            [id] => 3
            [ref] => 1
            [arr] => Array ()
        )
    [3] => Array
        (
            [id] => 4
            [ref] => 3
            [arr] => Array ()

        )
)

Here’s my code.

$myArr = array(/* above elements gathered from the DB */);
foreach ($myArr as &$arr) {
    if (!is_null($arr['ref'])) relocate($arr, $arr['ref']);
}
unset($arr);

function relocate(&$node, $id, &$arr=NULL) {
    global $myArr;
    if (is_null($arr)) $arr = &$myArr;
    foreach ($arr as &$arr1) {
        if ($arr1['id'] == $id) {
            array_push($arr1['arr'], &$node);
            return;
        }
    }
    unset($arr1); // not sure if this is necessary
    if (!empty($arr['arr'])) relocate(&$node, $id, &$arr['arr']);
}

And the desired output:

Array
(
    [0] => Array
        (
            [id] => 1
            [ref] => 
            [arr] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [ref] => 1
                            [arr] => Array ()
                        )
                    [1] => Array
                        (
                            [id] => 3
                            [ref] => 1
                            [arr] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [ref] => 3
                                            [arr] => Array ()
                                        )
                                )
                        )
                )
        )
)

What am I missing?

  • 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-12T00:16:57+00:00Added an answer on June 12, 2026 at 12:16 am

    I think moving by reference is an imaginary approach. I ended up using the following code.

    $myArr = array(/* elements gathered from the DB */);
    foreach ($myArr as $arr) {
        if (!is_null($arr['ref'])) {
            relocate($arr, $arr['ref']);
            unset($myArr[array_search($arr,$myArr)]);
        }
    }
    
    function relocate($node, $id, &$arr=NULL) {
        global $myArr;
        if (is_null($arr)) $arr = &$myArr;
        foreach ($arr as &$arr1) {
            if ($arr1['id'] == $id) {
                return array_push($arr1['arr'], $node);
            }
            if (!empty($arr1['arr'])) relocate($node, $id, &$arr1['arr']);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a randomly ordered C array of integer numbers. Each of these numbers
I have an ordered string that I need to present to a user: ABCCDDCBBBCBBDDBCAAA
I have sever JPanels which have to be ordered vertically. For that I want
I have two ordered lists next to each other. When I take a node
I have a website that lets people view rows in a table (each row
I have an array of records that is laid out in the following structure:
I have an array of Place objects. Each Place object has a name and
I have code already that loops through each member of my website and can
I have a rooted ordered tree representing sets of integers. Each node stores the
I have an array that looks something like: status[0] = true status[1] = true

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.