foreach iterates through the parent array, then iterates through the subarray, how to stitch the parent-child array

Source: Internet
Author: User
Description: The parent array contains a unique pid,pid by calling the M-layer to get the contents of the sub-array, requiring the parent and child arrays to have the same key and then merging them together
Code

foreach($parentArray as $v){    $a['id']=$v['pid'];    $a['name'] =$v['name'];    ...    $array[] =$a;    $childContent = $m->getChildContent($v['pid']);    foreach($childContent as $v2){        $b['id']=$v2['cid'];        $b['name']=$v2['cname'];        ...        $array2[]=$b;    }}

I want to make the output such a format

array[0]{    $array里的内容,    $array2里的内容}array[1]{   $array,   $array2,}

Because it is an infinite rating, you want to combine the first and child array groups of the foreach traversal array into an array,
Final output style:
Parent Parents Column
--child Sub-column
Parent Parents Column
--child Sub-column
Parent Parents Column
--child Sub-column
.....

Reply content:

Description: The parent array contains a unique pid,pid by calling the M-layer to get the contents of the sub-array, requiring the parent and child arrays to have the same key and then merging them together
Code

foreach($parentArray as $v){    $a['id']=$v['pid'];    $a['name'] =$v['name'];    ...    $array[] =$a;    $childContent = $m->getChildContent($v['pid']);    foreach($childContent as $v2){        $b['id']=$v2['cid'];        $b['name']=$v2['cname'];        ...        $array2[]=$b;    }}

I want to make the output such a format

array[0]{    $array里的内容,    $array2里的内容}array[1]{   $array,   $array2,}

Because it is an infinite rating, you want to combine the first and child array groups of the foreach traversal array into an array,
Final output style:
Parent Parents Column
--child Sub-column
Parent Parents Column
--child Sub-column
Parent Parents Column
--child Sub-column
.....

I don't know if you're talking about a situation that is similar to an infinite classification.
If parent and child are placed on the same table (should also be)
If there's only two floors, that's what I usually do.
Then the query is sorted by PID

ORDER BY `pid` ASC

Then iterate through the array

$result = array();foreach($data as $v) {    if($v['pid'] == 0) {        $result[$v['id']] = $v;        continue;    }    $result[$v['pid']]['chlid'][] = $v;}var_dump($result);

If you need three layers, for example,

array(    array(        'id',        'pid',        'child' => array(            array(                'id',                'pid',                'child' => array(                    ...                ),            ),        ),    ),)

In that case, I find this way a bit of trouble.

Resources:http://www.cnblogs.com/yangmanyan/archive/2011/06/16/2082963.html

Sorry to update, the results from the database query, even if the field type is integer type, query out is also a string.
So it should be $v['pid'] == 0 or $v['pid'] === '0' .

To be converted to: o:27: "Guzzlehttp\cookie\cookiejar": 2:

  • 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.