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?
Related
I am in too much trouble. I need below type of array:-
$val = "abc";
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
I tried below:-
for($i = 1; $i<16; $i++)
{
$arr.$i["besk"] = $val
}
here I've $val. so not to worry on that. But array is not properly creating. Any help would be appreciated.
first define the array as string
like
$arr = 'arr';
then use the foreach
like
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
You need to use variable variables (not recommended)
for($i = 1; $i<16; $i++)
{
${"arr".$i}["besk"] = $val
}
EDIT : #CBroe is right about his comment, you should use an array instead. So the best solution would be to create a two dimensional array like so :
$arr = [];
for($i = 0; $i<15; $i++)
{
$arr[$i]["besk"] = $val
}
The only difference is your array indexes start from 0 now and if you want to have the third value of your array you need this command $arr[2]["besk"]
it is very simple use this:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val
}
Use this approach:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
add new variable
$val = "abc";
$arrName = "arr"; //this one
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
and to call it
for($i = 1; $i<16; $i++)
{
${$arrName.$i}["besk"] = $val
}
ps. you did not create array, you just create 15 array variable with 1 index("besk" index)
I was wondering if it is possible to create unique variables in a for loop using PHP. I tried the following code but it didn't work:
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$level_ + $i = array();
}
I would like to end up with the variables $level_1, $level_2, $level_3, $level_4, $level_5 and $level_6. How would I achieve this?
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$l = "level" . $i;
$$l = array();
}
But Zerkms is right...
$arr = array(array(),array(),array(),array(),array(),array());
It's much easier if you use arrays for this. Try this one-liner:
$level_count = 6;
$array = array_fill_keys(array_map(function($index) {
return 'level_' . $index;
}, range(1, $level_count)), array());
var_dump($array);
Weird thing (I have no idea why you want to use it), but, just for educational purposes...
$level_count = 6;
for ($i = 1; $i <= $level_count; $i++) {
$name = 'level_' . $i;
$$name = array();
}
How can I store this in a $_var variable ?
$s_number = 5;
$spn = '6';
echo "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++)
{
echo "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
echo "]";
Question is bit vague though it seems probably you are looking for this,
$string = "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++) {
$string .= "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$string .= "]";
echo $string;
Other suggestions that use strings are fine, but I prefer to create arrays for tasks like this:
$s_number = 5;
$spn = '6';
$landscapes_array = array();
for ($i = 1; $i <= $s_number; $i++) {
$landscapes_array[] = "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$landscapes = "'Landscapes':[" . implode('', $landscapes_array) . "]";
You could also try putting the for loop in a function passing your variables and then return the values to a new variable. That's like saving a loop in a variable.
Hi I have a list of values named:
$value1
$value2
$value3
...
and I'd like to assign each value to an array element; something like:
$my_array[1]=$value1;
$my_array[2]=$value2;
$my_array[3]=$value3;
How can I do this using a for cycle? The array is not a problem but I can't figure out how to write some code for the value, it should be something like:
for($i=1; $i<=10000; $i++)
{
$my_array[$i]=$value$i;
}
Try this:
for ($i=1; $i<=10000; $i++) {
$val_name = "value" . $i;
$my_array[$i]=$$val_name;
}
You are almost there:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value;
}
Or this, if you want to append the counter as well:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value . $i;
}
What you are looking for are the {}.
$my_array[$i]=${'value'.$i};
for ($i = 1; isset(${"value$i"}); $i++) {
$my_array[$i] = ${"value$i"};
}
This syntax is known as variable variables.
You can use the $$ syntax:
for($i = 1; $i <= 10000; $i++) {
$name = 'value' . $i;
$my_array[$i] = $$name;
}
In My last post I asked :
How to create dynamic incrementing variable using "for" loop in php? like wise: $track_1,$track_2,$track_3,$track_4..... so on....
whose answer I selected as
for($i = 0; $i < 10; $i++) {
$name = "track_$i";
$$name = 'hello';
}
and
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
}
Now, What If I need the Value of variable previous than the current variable?
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if($i != 0){
$prev_val = ${'track_' . ($i - 1)}
}
}
But it's much better to use arrays for this, which are meant for this application.
$tracks = array();
for($i = 0; $i < 10; $i++){
$tracks[$i] = 'val';
if($i != 0){
$prev_val = $tracks[$i-1];
}
}
I guess the simples way would be to use two variables.
$name2 = "track_0";
for($i = 1; $i < 10; $i++) {
$name1 = $name2;
$name2 = "track_$i";
$$name1 = 'hello_previous';
$$name2 = 'hello_this';
}
Or if you explicitly use i = [0...10] to generate a variable name, you could simply write $name2 = "track_". $i; $name1 = "track_" . ($i - 1);
I know the others are saying just subtract by 1, but what if your list goes 1, 2, 4, 5, 8, 9? The previous of 8 is not 7, but 5, this following method (with a bit of modification to work as you require) will provide a way of getting the true previous value, and not a guessed one.
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if(!empty($last_val))
// do what you want here
// set a var to store the last value
$last_val=$i;
}
if ($i != 0)
{
$prev = ${'track_' . ($i-1)} ;
}
?
${'track_' . ($i-1)}; won't suffice?