摆脱foreach循环以返回单个记录

时间:2015-04-26 03:37:10

标签: php variables foreach

这应该很简单,但对于我作为初学者,我仍然在努力处理各种View显示调用。我从我的软件的另一部分借用了这个foreach循环,它运行正常。它返回页面上每条记录的值。但是,我只想要当前的记录。所以,我需要摆脱foreach循环,但无法弄清楚如何转换这个foreach循环只获取当前记录。

非常感谢你的帮助。

<?php echo $this->header_part; ?>

<div id="defaultcontainerwrapper" class="maxwidth">
    <?php foreach($this->challengenames as $k=>$challengename) { ?>
        <header>
            <h1>
                <div class="list">
                    <span>Welcome to </span><?php echo $challengename['partnerName']; ?><span>'s Beat Waste Challenge!</span>
                </div>
            </h1>
        </header>
    <?php } ?>
</div>

<?php echo $this->footer_part; ?>

这是来自控制器的功能:

public function yourChallengeAction(){
    //get activated challenge names and set variables
    $request = JO_Request::getInstance();
    $myChallenge=$this->getChallenge();
    $this->view->challengenames = array();
    foreach($myChallenge AS $k=>$challengename){
        $this->view->challengenames[$k]['partnerName'] =    $challengename['partnerName'];
        $this->view->challengenames[$k]['code'] = $challengename['code'];
        $this->view->challengenames[$k]['challengeTitle'] = $challengename['challengeTitle'];
        $this->view->challengenames[$k]['description'] = $challengename['description'];
        $this->view->challengenames[$k]['image_url'] = $challengename['image_url'];
    }

1 个答案:

答案 0 :(得分:0)

使用reset()或使用[0]作为数组键。

另见¶

current() - Return the current element in an array
each() - Return the current key and value pair from an array and advance the array cursor
end() - Set the internal pointer of an array to its last element
next() - Advance the internal array pointer of an array
prev() - Rewind the internal array pointer