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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:58:12+00:00 2026-05-25T11:58:12+00:00

I need a little help. My knowledge of algorithms are weak. I can not

  • 0

I need a little help. My knowledge of algorithms are weak. I can not write a recursive function in PHP that returns all the latest children.

Suppose our array looks like this:

Array
(
[0] => Array
    (
        [id_category] => 1
        [name] => Accueil
        [id_parent] => 0
    )

[1] => Array
    (
        [id_category] => 2
        [name] => Accessoires
        [id_parent] => 1
    )

[2] => Array
    (
        [id_category] => 3
        [name] => Merchandising
        [id_parent] => 1
    )

[3] => Array
    (
        [id_category] => 4
        [name] => Pièces détachées
        [id_parent] => 1
    )

[4] => Array
    (
        [id_category] => 5
        [name] => Excavateur
        [id_parent] => 4
    )

[5] => Array
    (
        [id_category] => 6
        [name] => série 100
        [id_parent] => 5
    )

[6] => Array
    (
        [id_category] => 7
        [name] => above
        [id_parent] => 6
    )

[7] => Array
    (
        [id_category] => 8
        [name] => système hydraulique
        [id_parent] => 7
    )

[8] => Array
    (
        [id_category] => 9
        [name] => série 200
        [id_parent] => 5
    )

[9] => Array
    (
        [id_category] => 10
        [name] => thru
        [id_parent] => 6
    )

[10] => Array
    (
        [id_category] => 11
        [name] => Compaction
        [id_parent] => 4
    )

[11] => Array
    (
        [id_category] => 12
        [name] => système électrique
        [id_parent] => 7
    )

)

I would like getLastChildren (5) or getLastChildren (6) or getLastChildren (7), the function returns me an answer array (“8”, “12”)

I will try to give an example.

If I take the category 5: = 6 and 9 are the children.

I look through children. The child 6 has two children, 7 and 10, Child 9: no children.

I put 9 in the list of children.

The child 7 has two children, 8 and 12. 8 has no children. 12 has no children. I add 8 and 12.

So we return (9,8,12) 10 has no children. I also added.

In the end I (9,8,12,10)

So what I would do, if I search “all the last children in category 7” => 8, and 12. I hope my explanation is “a little clearer.”

  • 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-25T11:58:13+00:00Added an answer on May 25, 2026 at 11:58 am

    I would do something like that:

    $array = array(
        array (
            'id_category' => 1,
            'name' => 'Accueil',
            'id_parent' => 0,
        ),
        array (
            'id_category' => 2,
            'name' => 'Accessoires',
            'id_parent' => 1,
        ),
        array (
            'id_category' => 3,
            'name' => 'Merchandising',
            'id_parent' => 1,
        ),
        array (
            'id_category' => 4,
            'name' => 'Pièces détachées',
            'id_parent' => 1,
        ),
        array (
            'id_category' => 5,
            'name' => 'Excavateur',
            'id_parent' => 4,
        ),
        array (
            'id_category' => 6,
            'name' => 'série 100',
            'id_parent' => 5,
        ),
        array (
            'id_category' => 7,
            'name' => 'above',
            'id_parent' => 6,
        ),
        array (
            'id_category' => 8,
            'name' => 'système hydraulique',
            'id_parent' => 7,
        ),
        array (
            'id_category' => 9,
            'name' => 'série 200',
            'id_parent' => 5,
        ),
        array (
            'id_category' => 10,
            'name' => 'thru',
            'id_parent' => 6,
        ),
        array (
            'id_category' => 11,
            'name' => 'Compaction',
            'id_parent' => 4,
        ),
        array (
            'id_category' => 12,
            'name' => 'système électrique',
            'id_parent' => 7,
        ),
    );
    

    I split data and code, here is the code:

    $children = array();
    function getLastChildren($array, $parent) {
    global $children;
        foreach ($array as $key => $value) {
            if ($value['id_parent'] == $parent) {
                if (hasChild($array, $value['id_category'])) {
                    getLastChildren($array, $value['id_category']);
                } else {
                    $children[] = $value['id_category'];
                }
            }
        }
    }
    
    function hasChild($array, $parent) {
        foreach ($array as $key => $value) {
            if ($value['id_parent'] == $parent) {
                return true;
            }
        }
        return false;
    }
    
    getLastChildren($array, 5);
    print_r($children);
    

    output:

    Array
    (
        [0] => 8
        [1] => 12
        [2] => 10
        [3] => 9
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help about a PHP FB app. Matter is that I
Hey guys, thanks for all the help that you can provide. I need a
Need a little help with my jquery here I want all my button with
I need a little help creating a catch-all error handling page in my ICEfaces
I have little knowledge of java scripting, and need help making my application work.
I am not a CSS guy as you can see and I need little
I need a little help on how I can enforce the following constraints on
Need a little help with a regex I'm trying to match a string that
I need a little help, I have a report that I am running that
Need a little help with a SQL / ActiveRecord query. Let's say I have

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.