I'm trying to stop $implode_demographics inside for loop below.
For example, if I use echo $implode_demographics inside loop, then I will get:
a01a01,a02a01,a02,a03a01,a02,a03,a04a01,a02,a03,a04,a05a01,a02,a03,a04,a05,a06a01,a02,a03,a04,a05,a06,a07a01,a02,a03,a04,a05,a06,a07,a08a01,a02,a03,a04,a05,a06,a07,a08,a09a01,a02,a03,a04,a05,a06,a07,a08,a09,a10a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02a01,a02,a....
But If I use this string outside, then output works.
a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02,b03,c01,c02,c03,c04,c05,c06,c07,c08,c09,c10,c11,c12,c13
Online PHP test
So, how I can echo $implode_demographics inside for loop to get same result?
$a_l_demographics = [11,3,13];
$a_p_demographics = ['a','b','c'];
$a_r_demographics = [];
$c_demographics_values = array();
$a_c_demographics = min(count($a_l_demographics), count($a_p_demographics));
for ($i = 0; $i < $a_c_demographics; $i++) {
for ($j = 1; $j <= $a_l_demographics[$i]; $j++) {
$a_r_demographics[] = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
$implode_demographics = implode($a_r_demographics, ',');
// this won't work
echo $implode_demographics;
}
}
// this works
// echo $implode_demographics;
It doesn't works because you implode the same array again and again after you add data in it.
So in your first echo you have only a01, then you implode after with a01 and a02 value, etc.
Try to add echo $implode_demographics . "\n"; and you will see that you have one var, then two, then three, etc. see example here : https://3v4l.org/WrCgJ
So if you want to echo each value INSIDE, just do :
<?php
$a_l_demographics = [11,3,13];
$a_p_demographics = ['a','b','c'];
$a_r_demographics = [];
$c_demographics_values = array();
$a_c_demographics = min(count($a_l_demographics), count($a_p_demographics));
for ($i = 0; $i < $a_c_demographics; $i++) {
for ($j = 1; $j <= $a_l_demographics[$i]; $j++) {
$a_r_demographics[] = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
$implode_demographics = implode($a_r_demographics, ',');
// You echo only the last value
echo $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);
// If it's not the last loop : you add a `,`
if (!($i == ($a_c_demographics - 1) && $j == $a_l_demographics[$i]))
echo ",";
}
}
This way you will echo only the LAST value each time and not all the value from the beginning each time, the output is :
a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,b01,b02,b03,c01,c02,c03,c04,c05,c06,c07,c08,c09,c10,c11,c12,c13
See code here : https://3v4l.org/MEnGe
In fact it's work, you have a problem with the implode. Implode d'ont put the glue (,) to the last value !
try this.
$implode_demographics = implode($a_r_demographics, ',');
echo $implode_demographics.', ';
I am making a website that has x amount of input fields. Each of the input fields is labeled team0, team1, team2 ... teamx and they are wrapped in a post form. Upon posting, I would like to check the POST variables, store them in an array, print out their values, but it seems I cannot use a variable when calling $_POST. I have tried it like so:
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team$i'];
echo $teamNames[$i] . "<br>";
}
What is the correct way to do this?
Try double quotes
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST["team$i"];
echo $teamNames[$i] . "<br>";
}
OR
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team'.$i];
echo $teamNames[$i] . "<br>";
}
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.
I need a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?