Hi,
I'm using the Joomla CMS and I encountered some PHP code I'm really struggling with. I'm hoping someone can help me. The code appears to loop through an array, but I discovered that it's not just a simple array. I discovered this when trying to iterate through it myself with a foreach loop. The loop failed, and I found that the item being iterated was an object??? So that means it's a little more complex. So i did a varDump and found out that it is indeed very, very complex. I just want to understand the foreach loop and what it's doing. I understand it as it pertains to a simple array, but this is not a simple array. Here is the code. (the switch statement was added by me)
foreach ($list as $i => &$item) :
$class = 'item-'.$item->id;
switch($item->id){
case 103:
if ((in_array($Itemid, array(109, 110, 111,112)))){$class .= ' active';} break;
case 104:
if ((in_array($Itemid, array(113, 115, 114)))){$class .= ' active';} break;
case 105:
if ((in_array($Itemid, array(116,117,118)))){$class .= ' active';} break;
default: break;
}
if ($item->id == $active_id) {
$class .= ' current';
}
if (in_array($item->id, $path)) {
$class .= ' active';
}
elseif ($item->type == 'alias') {
$aliasToId = $item->params->get('aliasoptions');
if (count($path) > 0 && $aliasToId == $path[count($path)-1]) {
$class .= ' active';
}
elseif (in_array($aliasToId, $path)) {
$class .= ' alias-parent-active';
}
}
if ($item->deeper) {
$class .= ' deeper';
}
if ($item->parent) {
$class .= ' parent';
}
if (!empty($class)) {
$class = ' class="'.trim($class) .'"';
}
//=====================================================================
echo '<li'.$class.'>';
// Render the menu item.
switch ($item->type) :
case 'separator':
case 'url':
case 'component':
require JModuleHelper::getLayoutPath('mod_menu', 'default_'.$item->type);
break;
default:
require JModuleHelper::getLayoutPath('mod_menu', 'default_url');
break;
endswitch;
// The next item is deeper.
if ($item->deeper) {
echo '<ul>';
}
// The next item is shallower.
elseif ($item->shallower) {
echo '</li>';
echo str_repeat('</ul></li>', $item->level_diff);
}
// The next item is on the same level.
else {
echo '</li>';
}
endforeach