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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:19:06+00:00 2026-05-25T19:19:06+00:00

I have an array that looks like this: Array ( [0] => Array (

  • 0

I have an array that looks like this:

Array (
    [0] => Array
    (
        [term_id] => 23
        [name] => testasdf
        [depth] => 1
    )
    [1] => Array
    (
        [term_id] => 26
        [name] => asdf
        [depth] => 2
    )
    [2] => Array
    (
        [term_id] => 31
        [name] => Another level deep
        [depth] => 3
    )
    [3] => Array
    (
        [term_id] => 32
        [name] => Another level deep
        [depth] => 2
    )
    [4] => Array
    (
        [term_id] => 24
        [name] => testasdf
        [depth] => 1
    )
    [5] => Array
    (
        [term_id] => 27
        [name] => asdf
        [depth] => 1
    )
)

Here is the recursive function that I’m using, it works except in some cases (where the depth is greater it seems.

function process(&$arr, &$prev_sub = null, $cur_depth = 1) {
    $cur_sub = array();
    while($line = current($arr)){
        if($line['depth'] < $cur_depth){
            return $cur_sub; 
        }elseif($line['depth'] > $cur_depth){
            $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );
        }else{
            $cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
            $prev_sub =& $cur_sub[$line['term_id']];
            next($arr);
        }
    }
    return $cur_sub;
}

This is how the results look:

Array
(
    [23] => Array
    (
        [26] => Array
        (
            [31] => Array
            (
                [term_id] => 31
                [name] => Another level deep
            )
        )
        [32] => Array
        (
            [term_id] => 32
            [name] => Another level deep
        )
    )
    [24] => Array
    (
        [term_id] => 24
        [name] => testasdf
    )
    [27] => Array
    (
        [term_id] => 27
        [name] => asdf
    )
)

Any idea how I can have it so the term_id and name is shown for all depths?

  • 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-25T19:19:06+00:00Added an answer on May 25, 2026 at 7:19 pm

    try this:

    function process(&$arr, &$prev_sub = null, $cur_depth = 1) {
    
      $cur_sub = array();
        while($line = current($arr)){
            if($line['depth'] < $cur_depth){
                return $cur_sub; 
            }
            if($line['depth'] > $cur_depth){
                $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );
    
            }
    
                $cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
                $prev_sub =& $cur_sub[$line['term_id']];
                next($arr);
        }
      return $cur_sub;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks like this: abc[1,2,3].something.here,foo[10,6,34].somethingelse.here,def[1,2].another I want to split this
I have an array that looks like this: Array ( [0] => Array (
Ok so I have an array that looks like this. [Enter Sandman, One, Nothing
I have an array that looks like this: var y = [1, 2, 3];
I have a array that looks like this: $sites = array('Twitter' => 'http://twitter.com/home?status=$status', 'Digg'
I have an array that looks like this. [{EntryId=>2, Field1=>National Life Group,DateCreated=>2010-07-30 11:00:14, CreatedBy=>tristanoneil},
I have one array that contains some settings that looks like basically like this:
I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have an array that looks like this: grades[0] = [A, 4.0, 0, a];
I have an array that looks like this: a = [ -22 347 4448

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.