【问题标题】:php: how to get each value from the arrayphp:如何从数组中获取每个值
【发布时间】:2013-09-06 07:44:45
【问题描述】:

我在下面的代码中获取数组值

$userdays_template=$this->templateroutinemodel->get_AllDays_by_templateRoutine_id($rid); 

然后我打印了(echo("---userdays_template--.var_dump($userdays_template));) 它给了我这样的输出:array(4) { [0]=> string(3) "965" [1]=> string(3) "964" [2]=> string(3) "959" [3]=> string(3) "958" }

所以我的问题是,如何在循环中从这个数组中获取每个值?...

我尝试了什么:

  $userdays_template=$this->templateroutinemodel->get_AllDays_by_templateRoutine_id($rid); 
    echo("---userdays_template---------".var_dump($userdays_template));
      if (is_array($userdays_template)) 
          {
          foreach ($userdays_template as $row) 
              {   
               $day_value= $row->day_id;;
                echo("---day---------".$day_value);//not printing the this line,why? 
             } 
         } 

但它没有打印这个回声(echo("---day---------".$day_value);)。请帮帮我

【问题讨论】:

  • 看起来你有一个数组,而不是一个对象。试试$day_value = $row;
  • @billyonecan 感谢您的回答帮助。它工作正常。只需在回答表单中回答您的评论,以便我可以接受您的回答,--majority 给你,因为你已经先回答了

标签: php codeigniter


【解决方案1】:

更改以下代码:

$day_value= $row->day_id;

使用以下代码:

$day_value= $row;
echo $day_value;

您有两个给出错误的半列。而且数组中没有 day_id 删除它。使用上面的代码。

【讨论】:

    【解决方案2】:

    用这个替换你的代码:-

    $userdays_template=$this->templateroutinemodel->get_AllDays_by_templateRoutine_id($rid); 
    if (is_array($userdays_template)) 
       {
         foreach ($userdays_template as $row) 
            {   
             echo("---day---------".$row);
            } 
       } 
    

    【讨论】:

      【解决方案3】:

      看起来你需要做的就是:

      foreach ($userdays_template as $row) 
      {   
           echo("---day---------".$row);
      } 
      

      【讨论】:

        【解决方案4】:

        您正在使用 foreach 作为关联数组,因此您可以通过两种方式执行您需要的操作
        第一个:

             foreach ($userdays_template as $key=>$val) 
             {   
                   $day_value= $val;
                   echo("---day---------".$day_value);
             } 
        

        或者使用 for 循环并使用数组索引 0,1,2,...

            for ($i = 0 ; $i < count($userdays_template))  
            {
              echo("---day---------".$userdays_template[$i]);
            }
        

        【讨论】:

          猜你喜欢
          • 2014-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多