简体   繁体   English

Joomla访问getModules数据结构

[英]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. 我可以在单个版本的getmodule上完成它,并且可以正常工作,因为它没有包装在数组中。

<?=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. PHP的reset()重置数组的内部指针并返回第一个元素。 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. 但是,如果出于明显的原因,如果JModuleHelper返回空数组,则此操作失败。 Unfortunately, it will not fail quietly, since reset() returns false when provided with an empty array. 不幸的是,它不会悄然失败,因为在提供空数组时reset()返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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