How can PHP loop be implemented without loops?

Source: Internet
Author: User
Printed array: {code ...} when foreach ($ kas $ v) {} is output, it is found that the first chapter also follows the loop {code ...} how can this problem be solved by repeating this field for only once in Chapter 1? {Code:

Array ([0] => Array ([name] => Chapter 1 [cname] => 1 Athena Saint Seiya) [1] => Array ([name] => Chapter 1 [cname] => 2... ))

When foreach ($ k as $ v) {} is output, it is found that chapter 1 follows the cycle

Chapter 1 Athena's Saint Seiya Chapter 2 is burning...

How can this problem be solved by repeating this field for only once in Chapter 1?

Chapter 1 Athena's Saint Seiya 2 is burning...
Array ([0] => Array ([name] => Chapter 1 [0] => Array ([cname] => 1 Athena Saint) [1] => Array ([cname] => 2 )))

Reply content:

Printed array:

Array ([0] => Array ([name] => Chapter 1 [cname] => 1 Athena Saint Seiya) [1] => Array ([name] => Chapter 1 [cname] => 2... ))

When foreach ($ k as $ v) {} is output, it is found that chapter 1 follows the cycle

Chapter 1 Athena's Saint Seiya Chapter 2 is burning...

How can this problem be solved by repeating this field for only once in Chapter 1?

Chapter 1 Athena's Saint Seiya 2 is burning...
Array ([0] => Array ([name] => Chapter 1 [0] => Array ([cname] => 1 Athena Saint) [1] => Array ([cname] => 2 )))


  'Chapter 1 ', 'cname' => '1 Athena Saint Seiya'], ['name' => 'Chapter 1 ', 'cname' => '2 burning Ba '], ['name' => 'Chapter 2', 'cname' => '1 burning Ba '], ['name' => 'Chapter 2 ', 'cname' => '2 universe'],]; $ result = []; foreach ($ a as $ chap) {$ name = $ chap ['name']; if (! Isset ($ result [$ name]) {$ result [$ name] = [];} $ result [$ name] [] = $ chap ['cname'];} print_r ($ result );

Output:

Array ([Chapter 1] => Array ([0] => 1 Athena Saint Seiya [1] => 2) [Chapter 2] => Array ([0] => 1 combustion bar [1] => 2 small universe ))

Changed to the answer that fully complies with the output of the subject.


  'Chapter 1 ', 'cname' => '1 Athena Saint Seiya'], ['name' => 'Chapter 1 ', 'cname' => '2 burning Ba '], ['name' => 'Chapter 2', 'cname' => '1 burning Ba '], ['name' => 'Chapter 2 ', 'cname' => '2 universe'],]; $ 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] => Chapter 1 [0] => Array ([cname] => 1 Athena Saint) [1] => Array ([cname] => 2 )) [1] => Array ([name] => Chapter 2 [0] => Array ([cname] => 1 burn it) [1] => Array ([cname] => 2 small universe )))

Should it be a subclass of Athena Saint Seiya?

I understand that a structure contains the chapter sequence number and Chapter name, and traverses the entire list. The same chapter sequence number is output only once.

Provide an idea to record the name of the last element, traverse the array element, and compare the name field of the element with the name field of the last element each time, otherwise, the output is recorded. The following is a pseudo code that is not a standard pseudocode:

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

Because the question has been 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.