user72425
user72425

Reputation: 25

php: how to get each value from the array

i getting array value the below code

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

And i printed( echo("---userdays_template--.var_dump($userdays_template));) it and it gave me output like: array(4) { [0]=> string(3) "965" [1]=> string(3) "964" [2]=> string(3) "959" [3]=> string(3) "958" }

So my question is ,how can i get each value from this array in a loop?...

What i tried:

  $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? 
             } 
         } 

but its not printing this echo(echo("---day---------".$day_value);). please help me

Upvotes: 1

Views: 103

Answers (4)

Omar Freewan
Omar Freewan

Reputation: 2678

You are using the foreach as associative array so you can do what you need in two ways
the first:

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

OR use the for loop and use the array index 0,1,2,...

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

Upvotes: 0

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

replace your code with this :-

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

Upvotes: 0

Mchl
Mchl

Reputation: 62377

Looks like all you need to do is:

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

Upvotes: 0

Code Lღver
Code Lღver

Reputation: 15603

Change your below code:

$day_value= $row->day_id;

With the following code:

$day_value= $row;
echo $day_value;

You have two semicolumn that give the error. And also there is no day_id in the array remove that. Use the above code.

Upvotes: 1

Related Questions