How does a PHP loop cycle through all the other loops?

Source: Internet
Author: User
Print out the array:

Array(    [0] => Array        (            [name] => 第一章            [cname] => 1 雅典娜的圣斗士        )    [1] => Array        (            [name] => 第一章            [cname] => 2 燃烧吧。。。        ))

When the output of foreach ($k as $v) {} is found, the first chapter is also followed by the loop

第一章  1雅典娜的圣斗士第一章  2 燃烧吧。。。

How to do the first chapter of this field only loop 1 times?

第一章  1雅典娜的圣斗士        2 燃烧吧。。。
Array(    [0] => Array        (            [name] => 第一章                    [0] => Array(                       [cname] => 1 雅典娜的圣斗士                    )                    [1] => Array(                       [cname] => 2 燃烧吧                    )        ))

Reply content:

Print out the array:

Array(    [0] => Array        (            [name] => 第一章            [cname] => 1 雅典娜的圣斗士        )    [1] => Array        (            [name] => 第一章            [cname] => 2 燃烧吧。。。        ))

When the output of foreach ($k as $v) {} is found, the first chapter is also followed by the loop

第一章  1雅典娜的圣斗士第一章  2 燃烧吧。。。

How to do the first chapter of this field only loop 1 times?

第一章  1雅典娜的圣斗士        2 燃烧吧。。。
Array(    [0] => Array        (            [name] => 第一章                    [0] => Array(                       [cname] => 1 雅典娜的圣斗士                    )                    [1] => Array(                       [cname] => 2 燃烧吧                    )        ))


  
    '第一章', 'cname' => '1 雅典娜的圣斗士'],    ['name' => '第一章', 'cname' => '2 燃烧吧'],    ['name' => '第二章', 'cname' => '1 燃烧吧'],    ['name' => '第二章', 'cname' => '2 小宇宙'],];$result = [];foreach ($a as $chap) {    $name = $chap['name'];    if (!isset($result[$name])) {        $result[$name] = [];    }    $result[$name][] = $chap['cname'];}print_r($result);

Output

Array(    [第一章] => Array        (            [0] => 1 雅典娜的圣斗士            [1] => 2 燃烧吧        )    [第二章] => Array        (            [0] => 1 燃烧吧            [1] => 2 小宇宙        ))

Modify the answer to fully match the main output of the title


  
    '第一章', 'cname' => '1 雅典娜的圣斗士'],    ['name' => '第一章', 'cname' => '2 燃烧吧'],    ['name' => '第二章', 'cname' => '1 燃烧吧'],    ['name' => '第二章', 'cname' => '2 小宇宙'],];$result = [];$nameMap = [];foreach ($a as $chap) {    $name = $chap['name'];    if (!isset($nameMap[$name])) {        $id = count($nameMap);        $nameMap[$name] = $id;    } else {        $id = $nameMap[$name];    }    if (!isset($result[$id])) {        $result[$id] = [];        $result[$id]['name'] = $name;    }    $result[$id][] = ['cname' => $chap['cname']];}print_r($result);

Output

Array(    [0] => Array        (            [name] => 第一章            [0] => Array                (                    [cname] => 1 雅典娜的圣斗士                )            [1] => Array                (                    [cname] => 2 燃烧吧                )        )    [1] => Array        (            [name] => 第二章            [0] => Array                (                    [cname] => 1 燃烧吧                )            [1] => Array                (                    [cname] => 2 小宇宙                )        ))

Burning it should be a subclass of Athena's Saints?

I understand the meaning is: a structure to hold the chapter number and chapter name, traverse the entire list, the same chapter ordinal output only once.

Provide a way of thinking, record the last name, iterate over the array elements, each time take an element to compare the name field of the element and the last name is the same, do not output, otherwise output and record, the following is not the standard pseudo-code pseudo-code:

string lastName = "";foreach(node in list){    if (lastName != node.name)    {        print(node.name);        lastName = node.name    }    else    {}        print(node.cname);}

Because the topic was changed:
NEW:

foreach (node in chaList){    print(name);    foreach (sec in secList)    {        print(cname);    }}
  • 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.