1

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

2
  • 1
    Looks like you have an array, not an object. Try $day_value = $row; Commented Sep 6, 2013 at 7:47
  • @billyonecan Thanks your answer helped. Its working fine. Just answer your comment in a Answer form, so that i can accept your answer, --majority goes to u , becoz u have answered first Commented Sep 6, 2013 at 7:52

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like all you need to do is:

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

Comments

0

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);
        } 
   } 

Comments

0

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]);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.