English 中文(简体)
PHP在物体中的使用
原标题:PHP loop in object

我有4个目标:

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

我的问题是,我不必打上同样的打字,而我可以把它放在这样的 lo中:

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

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

}

谢谢。

最佳回答

你可以这样做:

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

然而,我强烈建议使用阵列而不是动态特性。 如果它们不是动态的,那么它们不会有活力。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签