garek007 Posted July 5, 2012 Share Posted July 5, 2012 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 Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted July 5, 2012 Share Posted July 5, 2012 Iterating through an object is the same as iterating through an array with a few limitations. One of which is objects are strict on their structure where with arrays you can use variables as keys. Your loop fails, likely in part to you requesting the wrong property against an object. if (in_array($item->id, $path)) { $class .= ' active'; } Since $item->id is an object, you cannot in_array() on it. You could, however, if you converted it to an array with get_object_vars(). But depending on what you're actually looking for (hard to tell with no comments *tsk tsk*), you could check if your $item->id->path is equal to whatever it is that $path is. Quote Link to comment Share on other sites More sharing options...
garek007 Posted July 5, 2012 Author Share Posted July 5, 2012 Mahngiel. Sorry I should have specified. ignore the first Itemid (capital I). That is accessing the Joomla database. The second itemid (lowercase I) is used to go through the object. That foreach loop actually works fine, it was default and I am still trying to modify it. It works fine, but I just want to understand what is happening. Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted July 5, 2012 Share Posted July 5, 2012 t I just want to understand what is happening. At which point in the script? The entire thing??? Quote Link to comment Share on other sites More sharing options...
garek007 Posted July 5, 2012 Author Share Posted July 5, 2012 No worries, I figured out a different way to accomplish what I needed. I imagine I'll read about this and eventually learn what I need to know. Thanks for your help though! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.