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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?