Yii Framework Forum: Loop Through Array Vertically - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Loop Through Array Vertically Rate Topic: -----

#1 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 11 October 2012 - 07:05 AM

I have an array which contains data retrieved from a database table.

I know that I can retrieve the values by a normal foreach loop like this which is then going through the array row by row.

Quote

foreach ($arr as $val) {
echo $val->date.<br/>;
echo $val->hour1.<br/>;
echo $val->hour2.<br/>;
}



However, I am using a jquery plugin which requires me to print out the values vertically instead. I'm trying this:

Quote

// Columns are 2012-10-11 and 2012-10-12 and the data below goes straight under each column
foreach ($arr as $val) {
$date = $val->date;
foreach ($date as $v) {
echo '['.$v->hour1.', '.$v->hour1.'],'; // Each day shows hour 1
echo '['.$v->hour2.', '.$v->hour2.'],'; // Each day shows hour 2
}
}



This is not working. Any ideas?
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,562
  • Joined: 04-March 10
  • Location:UK

Posted 11 October 2012 - 09:40 AM

I'm not quite sure what you're asking and it's not clear from the code how your data is structured or what you're trying to do. Can you try to explain it again?
0

#3 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 11 October 2012 - 10:04 AM

You can try something similar to this:

foreach (array('date', 'hour1', 'hour2') as $field) {
  foreach ($arr as $val) {
    echo $val->$field;
  }
}

0

#4 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 11 October 2012 - 05:06 PM

View PostRodrigo Coelho, on 11 October 2012 - 10:04 AM, said:

You can try something similar to this:

foreach (array('date', 'hour1', 'hour2') as $field) {
  foreach ($arr as $val) {
    echo $val->$field;
  }
}



Thank you so much Rodrigo. That was exactly what I needed.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users