I have an array of topics. Some of them contain parent or child topics. I want to echo each topic even if it is a child. I loop through the array of topics, then check if the topic value is an array. If yes, then again loop through it and assign the topic to the topicName variable. If no, then assign the topic to the variable without looping. However, when I echo topicName it only echoes the ones in the else statement and it doesn't echo all of the topics. Can someone explain to me why it doesn't?
<?php
$topics = [
'Opening',
'Aanwezigen',
'Opmerkingen vorige notulen',
'Ingezonden stukken' => [
'Jaarverslag 2019',
'Jaarcijfers 2019',
],
'Jaarrekening 2019' => [
'Voorstel tot vaststelling van de jaarcijfers 2019 (stempunt)',
'Vaststelling dividenduitkering (stempunt)',
'Bespreking reserverings- en dividendbeleid',
],
'Rondvraag',
'Afsluiting',
];
foreach ($topics as $topic) {
if (is_array($topic)) {
for($i = 0; $i < count($topic); $i++) {
$topicName = $topic[$i];
}
} else {
$topicName = $topic;
}
echo $topicName;
}
?>
if (is_array($topic))get you everything? Wouldn't it be nested many levels deep?$topicsis only 2 levels deep. What if it becomes more than2?