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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:08:51+00:00 2026-05-12T05:08:51+00:00

I have this array: Array ( [1] => animal [1-1] => turtle [1-1-1] =>

  • 0

I have this array:

Array
(
    [1] => animal
    [1-1] => turtle
    [1-1-1] => sea turtle
    [1-1-2] => box turtle
    [1-1-3] => green turtle
    [1-1-3-1] => green turtle with brown tail
)

and I want some how to convert it into:

Array
(
    [1-title] => animal
    [1-sons] => array(
            [1-1-title] => turtle
            [1-1-sons] => array(
                    [1-1-1] => sea turtle
                        [1-1-2] => box turtle
                    [1-1-3-title] => green turtle
                    [1-1-3-sons] => array(
                            [1-1-3-title] => green turtle
                                  )
                    )
              )
)

or maybe you can suggest a better way for organizing the result array..

but how to do that?

I know that’s not easy task at all, I’m writing a parser that will walk on data and make tree out of them.

  • 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-12T05:08:51+00:00Added an answer on May 12, 2026 at 5:08 am

    The easiest way of organizing your data would be in such a way:

    array (
      'Animal' =>
      array (
        'Turtle' =>
        array (
          'Sea Turtle',
          'Box Turtle',
          'Green Turtle' =>
          array (
            'Green Turtle With Brown Tail',
          ),
          'Common Turtle',
        ),
      ),
    );
    
    // Or, otherwise written (equivalent to the above)
    
    $animals = array();
    $animals['Animal'] = array();
    $animals['Animal']['Turtle'] = array();
    $animals['Animal']['Turtle'][] = 'Sea Turtle';
    $animals['Animal']['Turtle'][] = 'Box Turtle';
    $animals['Animal']['Turtle']['Green Turtle'] = array();
    $animals['Animal']['Turtle']['Green Turtle'][] = 'Green Turtle With Brown Tail';
    $animals['Animal']['Turtle'][] = 'Common Turtle';
    

    Essentially, the name of the animal is the value, unless it has children, then the value is an array and the key is the animal name.


    That way, you can easily parse the values by doing the following:

    parse_animals($animals);
    
    function parse_animals($array, $indent = 0) {
      if(!is_array($array)) return;    // A little safe guard in case.
    
      foreach($array as $key => $value) {
        echo str_repeat('  ', $indent) . "- ";
    
        if(is_array($value)) {
          echo $key . "\n";
          parse_animals($value, $indent + 1);
        } else {
          echo $value . "\n";
        }
      }
    }
    

    The above in the console will output the following:

    - Animal
      - Turtle
        - Sea Turtle
        - Box Turtle
        - Green Turtle
          - Green Turtle With Brown Tail
        - Common Turtle
    

    EDIT: And here is a version that will output it for a webpage.

    function parse_animals_web($array) {
      if(!is_array($array)) return;    // A little safe guard in case.
    
      foreach($array as $key => $value) {
        echo '<ul>';
    
        if(is_array($value)) {
          echo '<li>' . htmlentities($key) . "</li>";
          parse_animals_web($value);
        } else {
          echo '<li>' . htmlentities($value) . "</li>";
        }
    
        echo '</ul>';
      }
    }
    

    The output is:

    • Animal
      • Turtle
      • Sea Turtle
      • Box Turtle
      • Green Turtle
        • Green Turtle With Brown Tail
      • Common Turtle

    Maybe you want to get the children of an animal.

    function get_children_of($array, $name) {
      foreach($array as $key => $value) {
        if(is_array($value)) {
          if($key === $name) {
            return $value;
          } else {
            return get_children_of($value, $name);
          }
        }
      }
    
      return array();
    }
    

    Now we can get all the children of the Green Turtle and output them.

    $green_turtle = get_children_of($animals, 'Green Turtle');
    parse_array($green_turtle);
    

    The output is:

    - Green Turtle With Brown Tail
    

    EDIT: Since you say you are stuck with the input array being in that weird format, here is a function that will convert your array into the format I specified above:

    function convert_array($array) {
      $new_array = array();
    
      $keys = array_keys($array);
      foreach($keys as $key) {
        $level = explode('-', $key);
        $cur_level = &$new_array;
        $cur_key = '';
    
        foreach($level as $o_key) {
          $cur_key = ltrim($cur_key . '-' . $o_key, '-');
          $next_key = $cur_key . '-1';
          $value = $array[$cur_key];
          $has_child = array_key_exists($next_key, $array);
    
          if($has_child) {
            if(!array_key_exists($value, $cur_level)) {
              $cur_level[$value] = array();
            }
            $cur_level = &$cur_level[$value];
          } else {
            $cur_level[] = $value;
          }
        }
      }
    
      return $new_array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this array that I am needing to insert into the database. My
I have this array of names (listedName) that I want to filter out and
I have this array and I want to get both ID and Description from
I have an array that sets a number for each animal. I want to
I have this array property in my model and would like to see it
I have this array {$man_data} which is structured like 10 > 'Text 8' 14
i have this array double a[][] = {{1,1,1}, {0,1,1} , { 1,0,0} ,{0,1,0},{1,0,0},{1,0,1},{1,1,1},{1,1,1},{1,0,1},{0,2,0},{0,1,1}}; and
I have this array: $pets = array( 'cat' => 'Lushy', 'dog' => 'Fido', 'fish'
I have this array in PHP: array( [0] => array( 'username' => 'user1' )
I have this array, for example (the size is variable): x = [1.111, 1.122,

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.