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