dongya3627 2016-03-03 07:20
浏览 50
已采纳

函数Yii Framework中的数组

can someone tell me what is this code doing, As im new to Yii, learning about it.. im not able to understand few things.. Here is the code..

$allmsg = LogMsg::model()->findAll($criteria); // 
    $dataArr = array();
    if (isset($allMsg) && sizeof($allMsg) != 0):
        foreach ($allMsg as $msg) {
            $dataArr[$msg->date][] = array( // array?
                'category' => $msg->category, // what is that 'category' a variable or something else? and $msg->category, is what?
                'time' => $msg->time,
                'date' => $msg->date,
                'user' => $msg->name
            );
        }   endif; 
$this->render('index', array(
            'data' => $dataArr ) //what is that 'data'?
    );

My question is, what is this line of code doing exactly in foreach loop

$dataArr[$msg->date][] = array(
                'category' => $msg->category,

and here is second code... which has something like that..

$allCat = Categories::model()->findAll($criteria);
    $catArr=array();
    if(isset($allCat) && sizeof($allCat)!=0):
        foreach ($allCat as $catModel) {
            $catArr[$catModel->id] =$catModel;
        }
    endif;
    return $catArr;

so what is this line doing in this code in foreach loop, what is different between these two lines in first and second code..

$catArr[$catModel->id] =$catModel;

last thing.. what is it

    public static function getID($category)
{
    $arr = array(
        'ast'=>1, // what are these things? from where are they coming? db?
        'fp'=>5, //
        'per'=>3, 
        'ts'=>6,
        'lg'=>3
    );
    return isset($arr[$category])?$arr[$category]:null;  //Ternary - Condensed if/else statement
}
  • 写回答

3条回答 默认 最新

  • douba9654 2016-03-04 06:41
    关注

    this is creating multidimensional array.

    Your first question

    $dataArr[$msg->date][] = array(
                    'category' => $msg->category,
    

    will generate output like

    [2016-03-04] => Array
            (
                [0] => Array
                    (
                        [category] => abc
                    )
    
            )
    

    And your second question

    $catArr[$catModel->id] =$catModel;
    

    will genrate output like

    array(
         [0] =>1,
        [1] => 2,
        [2] => 3,
    )
    

    Not tested.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办