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)
Related
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 to create array from variable php using loop for ?
i have many variable php like
$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;
i want to create array like this
$ar = array('1','2','5','2','6');
but using loop for like
for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
Not a recomended way of doing things but:
$arr = array();
for($i=0;$i<5;$i++) {
$varName = 'number_'.$i;
$arr[] = $$varName;
}
for ($i = 1; $i <= 6; $i++)
$ar[] = $i;
for ($i=1;$i<7;$i++)
{
$ar[] = $i;
}
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.
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?
I have two arrays like this
array x [Firefox,IE,Chrome,Opera]
array y [40,30,25,5]
Required final [[Firefox,40],[IE,30],[Chrome,25],[Opera,5]]
I need this in PHP.I think I can run a for loop and do something like this .
final23 [0][i] = x[i];
final23 [1][i] = y[i];
Is there any better way or built in function in PHP ?
$result = array();
$size = max(count($x), count($y));
for ($i = 0; $i < $size; $i++) {
$result[] = array(
isset($x[$i]) ? $x[$i] : null,
isset($y[$i]) ? $y[$i] : null
);
}
$x = array("Mozzila","IE","Firefox","Opera");
$y = array(40,30,25,5);
$final = array();
$i = 0;
foreach($x as $a){
$final[] = array($a,$y[$i]);
$i++;
}
Learn about Associated array.
$arr = new Array("40"=>"FireFox","30"=>"IE","25"=>"Chrome","5"=>"Opera");