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); }}