多数组迭代

时间:2012-10-24 07:57:14

标签: php arrays

您好我有一个功能:

public function getpopularAction()
{
    $businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
    $result = $businessReviewMapper->getTotalPopular();

    for ($i = 0; $i < count($result); $i++)
    {
        $rotd[$i] = $businessReviewMapper->getROTD($result[$i]['review_id']);     

        for ($j = 0; $j < count($rotd); $j++)
        {
            $rotd[$j]['u_img'] = $this->view->getLoginUserImage(
                $rotd[$j]['social_id'], $rotd[$j]['login_type'], null, null, large
            );
        }
     }

     print_r($rotd); 
     exit;
}

我得到的结果是:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [review_id] => 161
                    [review_desc] => tgi goooood....................
                    [user_id] => 2
                    [rating] => 3
                    [review_date] => 20121022203529
                    [name] => zlippr
                    [social_id] => 12345678
                    [login_type] => facebook
                    [user_unique_name] => zlippr
                    [city] => Kuala Lumpur
                    [business_name] => TGI Friday Kuala Lumpur
                )

            [u_img] => /public/images/image_not_found.png
        )
)

我不知道问题在哪里,但 u_img 未正确获取,不确定数组循环是否正确执行。

2 个答案:

答案 0 :(得分:0)

不确定内循环的用途。如果您正在尝试检索与每个rotd对应的loginuserimage,则需要执行以下操作:

    for ($i = 0; $i < count($result); $i++)
    {
       $rotd[$i] = $businessReviewMapper->getROTD($result[$i]['review_id']);     

       $rotd[$i]['u_img'] = $this->view->getLoginUserImage(
            $rotd[$i]['social_id'],$rotd[$i]['login_type'],null,null,large);
    }

答案 1 :(得分:0)

通过查看输出,您的结果位于数组的第二层内,这意味着您应该替换getLoginUserImage中的变量

$rotd[$i]['social_id'],$rotd[$i]['login_type'] 

$rotd[$i][0]['social_id'],$rotd[$i][0]['login_type']