简体   繁体   中英

Joomla Accessing getModules datastructure

From this

jimport( 'joomla.application.module.helper' );
$modules = JModuleHelper::getModules( 'top' );
echo '<pre>';
print_r( $modules );
echo '</pre>';

which outputs this structure

Array
(
    [0] => stdClass Object
        (
            [id] => 25
            [title] => Newsflash
            [module] => mod_newsflash
            [position] => top
            [content] => 
            [showtitle] => 1
            [control] => 
            [params] => catid=3
style=random
items=
moduleclass_sfx=
            [user] => 0
            [name] => newsflash
            [style] => 
        )

)

I currently call this function to get it to output it

<?=$modules[0]->content ?>

I would like to call with one line but it doesnt work

<?=JModuleHelper::getModules( 'top' )[0]->content ?>

I can do it on the singular version of getmodule and it works, it works because its not wrapped in an array.

<?=JModuleHelper::getModule( 'top' )->content ?>

Anybody know how to drill down on this data structure with one line of code?

PHP's reset() resets the internal pointer of the array and returns the first element. Therefore,

$modules = reset(JModuleHelper::getModules( 'top' ))->content;

will get the first element's content.

This will fail, however, if an empty array is returned by JModuleHelper , for obvious reasons. Unfortunately, it will not fail quietly, since reset() returns false when provided with an empty array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM