Jump to content

Please help me understand foreach and iterating through an object array.


garek007

Recommended Posts

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

Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.