How can I obtain the Subid function under multiple parent IDs?

Source: Internet
Author: User
How can I obtain the Subid function under multiple parent IDs? I am working on a project. I need multiple parent IDs to query the record of the Face ID under the project. I have written a function that can only query the records under a single parent ID. I don't know how to write these records, no idea. could you help me? thank you! Here are my own functions:
function get_child_id($data, $parent_id = '0') {        static $child;        foreach ($data as $value) {            if ($value['parent_id'] == $parent_id) {                $child .= ',' . $value['cat_id'];                $this->get_child_id($data, $value['cat_id'], $level + 1);            }        }        return $child;    }


Reply to discussion (solution)

You can call this method cyclically.

$parent_ids = array(1,2,3,4);$resul t= array();foreach($parent_ids as $parent_id){    $result[] = get_child_id($data, $parent_id);}function get_child_id($data, $parent_id = '0') {        static $child;        foreach ($data as $value) {            if ($value['parent_id'] == $parent_id) {                $child .= ',' . $value['cat_id'];                $this->get_child_id($data, $value['cat_id'], $level + 1);            }        }        return $child;    }

Can I call get_child_id for each parent ID?

However, static $ child; will cause problems in multiple calls

Multiple cycles get_child_id will repeat the subtopic ID

#2 I already told you that something went wrong
The crux is static $ child ;!

    function get_child_id($data, $parent_id = '0') {        $child = '';        foreach ($data as $value) {            if ($value['parent_id'] == $parent_id) {                $child .= ',' . $value['cat_id'];                $child .= $this->get_child_id($data, $value['cat_id'], $level + 1);            }        }        return $child;    }

I found this, but there will still be repeated subtopic IDs.

Paste your test data. the id is not unique (no endless loop is good for you)

Paste your test data. the id is not unique (no endless loop is good for you)


Paste your test data. the id is not unique (no endless loop is good for you)
That's the static problem. too many pages are opened and debugging is wrong. sorry, moderator!

I #4 didn't I give you the code?

I #4 didn't I give you the code?


Now, debugging is complete. Thank you moderator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.