PHP loop in object - php

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.

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

$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.

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

Related

Not getting dynamic variable names PHP

I want to print the values of variable but it prints 12 only
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}
This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}

Calling a variable from string in PHP

i need to call similar variable on shortest way.
$test = new stdClass();
$test->var0=0;
$test->var1=1;
$test->var2=2;
And now i need to echo all 3 variables in for cycle, not like this:
echo $test->var0;
echo $test->var1;
echo $test->var2;
Try this -
for($i = 0; $i < 3; $i++) {
echo $test->{'var' . $i};
}

How to return the object value within for loop?

Okay so i am trying to retrieve the object value iterating through a for loop.
Manually it looks like this:
echo $game->stats->item0;
echo $game->stats->item1;
...
I want to do it something like this:
for($i = 0; $i < 6; $i++) {
echo $game->stats->item.$i;
}
The above however just returns the value of $i. How can i return the actual object value?
Thanks
Basically, your statement should be like this:
echo $game->stats->{'item'.$i}
Regards,
Instead of item.$i, use {"item$i"}.
for($i = 0; $i < 6; $i++) {
echo $game->stats->{"item$i"};
}
Another way you can do it is to set a variable to be equal to 'item' . $i.
for($i = 0; $i < 6; $i++) {
$item = 'item' . $i;
echo $game->stats->$item;
}

PHP variables in a complex way

<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}

Display PHP Array values outside 'for loop'

Here is my code
for ($i=0; $i<$Percentile["Parameter_length"]; $i++)
{
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
The above code will display
Eqt_Param0=2.00
Eqt_Param1=3.00
Eqt_Param2=1.00
Eqt_Param3=5.00
If I put echo() outside the for loop, I need the same result. Please help me to fix this...
How about this? Simply concatenating the result of the foreach into a variable which can be echoed.
$output = "";
for ($i = 0; $i < $Percentile["Parameter_length"]; $i++)
{
$output .= "Eqt_Param" . $i . "=" . $Percentile["Eqt_Param" . $i];
}
echo $output;
You need to store your values in a viarable that exists outside of the scope of the for
like:
$accumulatedString = '';
for ($i=0; $i<$Percentile["Parameter_length"]; $i++) {
echo "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
$accumulatedString .= "Eqt_Param".$i."=".$Percentile["Eqt_Param".$i];
}
echo $accumulatedString;
Thats if you want it all as one string.

Categories