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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:53:29+00:00 2026-06-09T05:53:29+00:00

My problem.. I have a PHP array that looks like this: [1013] => [1154]

  • 0

My problem.. I have a PHP array that looks like this:

[1013] => [1154]
[1013] => [1322]
[1154] => [1525]
[1525] => [1526]

How can I take that and move it to something like this:

[1013] => [1154] => [1525] => [1526]
[1013] => [1322]

So it sort of makes a tree that associates to the top level array item. I do not have control over how the data comes to me, it’s generated through a third-party API and given to me just like that.

Logic: Client 1013 is the main account. Client 1154 is a client of 1013. Client 1322 is a client of 1013. Client 1525 is a client of 1154. I want to get it to a multi-dimension array so I can show this in a tree format.

  • 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-09T05:53:31+00:00Added an answer on June 9, 2026 at 5:53 am

    Here you go!:

    <?php
    // dataset
    $clientset = array(
      array(1013, 1154),
      array(1013, 1322),
      array(1154, 1525),
      array(1525, 1526)
    );
    
    $children = array();
    
    // make an array with children to see which nodes have none
    foreach($clientset as $set) {
      if(!isset($children[$set[0]])) $children[$set[0]] = array($set[1]);
      else $children[$set[0]][] = $set[1];
    }
    
    // array with parents
    $parents = array();
    foreach($clientset as $set) {
      $parents[$set[1]] = $set[0];
    }
    
    // for each node with no children, begin the search!
    foreach($clientset as $set) {
      if(!isset($children[$set[1]])) {
      echo getPath($set[1]).'</br>';
      }
    }
    
    // recursively search to parents and print them
    function getPath($child) {
      global $parents;
      if($parents[$child]) {
        return (getPath($parents[$child]).' => '.$child);   
      } else return $child;
    }
    ?>
    

    This outputs:

    1013 => 1322
    1013 => 1154 => 1525 => 1526
    

    The idea is to see which nodes have no children. Then, iterate through their parents. You probably don’t need the output like it is right now, but I’m sure you can work it out this way. Enjoy!

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

Sidebar

Related Questions

I have an PHP array that looks something like this: Index Key Value [0]
so I have this problem I have 3 files like 2.php <?php $variable =
I have an XML document that looks like this: <Data xmlns=http://www.domain.com/schema/data xmlns:dmd=http://www.domain.com/schema/data-metadata > <Something>...</Something>
I have a javascript object that looks somthing like this: var obj = {
I have a column chart in Highcharts that looks roughly like this: | |
I have an array $attribGarden which looks like this: array(3) { [0]=> array(2) {
I currently have a problem. I have a code that looks like the one
I realized that I can have problems with single quotes in php arrays: <?php
I have PHP script works without problems, like this : while($row4 = mysql_fetch_array($result4)) {
I have problem with php script. The thing is that it shows me, that

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.