0

I have 4 instances of the same object:

echo $payment->field_course_1_length[0]['value'];
echo $payment->field_course_2_length[0]['value'];
echo $payment->field_course_3_length[0]['value'];
echo $payment->field_course_4_length[0]['value'];

My question is so that I don't have to be typing the same over and over can I put it in a loop like this:

for ($i=1; $i<5; $i++) {

echo $payment->field_course_'$i'_length[0]['value'];

}

Thank you.

1
  • 1
    Yes, but syntax is wrong Commented Jan 12, 2012 at 13:26

3 Answers 3

3
echo $payment->{"field_course_{$i}_length"}[0]['value'];
Sign up to request clarification or add additional context in comments.

Comments

2
$tmp = $payment->{'field_course_' . $i . '_length'};
echo $tmp[0]['value'];

However, I strongly recommend to use arrays instead of dynamic properties. If they are not dynamic, don't access them dynamic.

Comments

1

You can do like this:

 for($i = 1;  $i <= 4; $i++) {
     echo $payment->{"field_course_".$i."_length"}[0]['value'];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.