Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBLESSING TASHINGA GURENJE
5,793 Pointslooping
Here is a multi-dimensional list of musical groups. The first dimension is group, the second is group members.
Can you loop through each group and output the members joined together with a ", " comma space as a separator, please?
1 Answer
jamesjones21
9,260 PointsHi Blessing, you sure can loop through a multidimensional array and output each group member separated by a comma, we can use the built in function that php handles a string which is implode. We can then delimiter each value within the members array by a comma as such implode(",", $member)
Please see my code below:
$groups = [
'Group1' => [
'Members' => ['James', 'Jonathan', 'Lewis'],
],
'Group2' => [
'Members' => ['Jordan', 'Bill', 'Michael'],
],
];
foreach($groups as $key => $value){
echo $key . '</br>';
foreach($value as $group => $members){
echo 'Members are: ' . implode(",", $members) . '</br>';
}
}