Create arrays with cycles - php

I have the following code:
for ($i = 1; $i <= $j; $i++)
{
$goods_{$i} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
I hoped that it could make this in first step of the cycle:
$i =1;
$goods_1 = array(
$_POST['goods1_title'],
$_POST['goods1_package'],
$_POST['goods1_nmr']
);
and so on in other steps.

I follow AbraCadaver's sentiments:
Why in the world are you doing this? You are using arrays, keep using them.
As such, I would write the code simply using an Array:
$goods = array();
for ($i = 1; $i <= $j; $i++)
{
// Assign to an index in the already created array,
// but DO NOT create a new variable.
$goods[$i] = array(
// Also make sure these are correct ..
$_POST["goods{$i}_title"],
);
}
If you really want to create dynamic variables - ick! - see variable variables.

Should be
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]

It can be done like this:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
You can read more about this topic in related PHP documentation.
The result of "'goods'.$i'_title'" will be 'goods'.1'_title', in case that you want it to be goods1_title then use following code instead:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]
);
}
Another bug might be that in 1 case you use .$i. and in other 2 cases you use .$i without the last ..

Related

Declaring a variable dynamically in PHP

I need some variables in the following format:
$m_01;
$m_02;
$m_03;
.....
.....
.....
$m_12;
The digits in the variables are the months of the calendar.
I can declare 12 variables separately. However, I want to declare the variable using a loop. So I did something like this.
for($i = 1; $i <= 12; $i++) {
if($i<10)
$month = '0'.$i;
else
$month = $i;
$m_$i;
}
However, I am getting some error:-
Parse error: syntax error, unexpected '$month' (T_VARIABLE) in /var/www/html/custom/ci/Dotell_customer/application/controllers/login.php on line 412
How can I overcome this issue?
NOTE:
$$month creates a variable 01;
Is there any way where I can cave variable m_01?
P.S. I am aware of array. I just want to learn PHP variables variables.
This is not the best practise to follow, but still just for the answer:
for($i = 0; $i <= 12; $i++) {
${"m_$i"} = $i;
}
echo $m_1;
echo $m_2;
echo $m_3;
The best practise would be to create an array. For eg:
$arr = [];
for( $i=0; $i <=12; $i++ ) {
$arr['m_'.$i] = $i;
}
print_r($arr);
I think you are looking for something like
$varname = 'm_'.$i; // format the way you need it
$$varname = ...
As noted in the comments, it's rather a code smell, and often unnecessary. Typically an array does a better job, i.e.
$m[$i] = ...
Use PHP variable variables. This works as also suggested in the comments
${"m_$i"}
Going with Davids suggestion of using an array.
$month = [];
for($i = 1; $i <= 12; $i++)
{
if($i<10)
$month[] = '0'.$i;
else
$month[] = $i;
$month[$i];
}
here the $var[] = ... syntax appends your array. the $month[$i] accesses it.
Use of double variable is not usually suggested. You should really use an array to solve your problem as:
$month = array();
for($i = 1; $i <= 12; $i++)
{
if($i<10)
$month['m_0'.$i] = $i;
else
$month['m_'.$i] = $i;
}
otherwise if you really want to necessary go for double variable then :
for($i = 1; $i <= 12; $i++)
{
if($i<10)
${"m_0$i"} = $i;
else
${"m_$i"} = $i;
}
I hope it helps
for($i = 1; $i <= 12; $i++){
if($i<10)
$month = '0'.$i;
else
$month = $i;
$month = 'm_' . $month;
..........
}
Access var as
assignment $month = 'm_12';
value $$month

How to change the value with a variable within a for loop?

I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";

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

get dynamic $_POST data into a for loop

for ($i = 0; $i <= ${"ConteggioColonneTabella".$NomeTabella}; $i++) {
${'record'.$i} = addslashes($_POST['record'].$i);
}
Look at
addslashes($_POST['record'].$i);
This isn't correct, i know, but I can't figure out what is the correct syntax for getting POST variables.
================= EDIT ==================
This works outside the loop
$i = 1;
${'record'.$i} = addslashes($_POST[record.$i])
getting only one result
this doens't work inside the loop, getting no results
for ($i = 1; $i <= 6; $i++) {
${'record'.$i} = addslashes($_POST['record'].$i);
}

PHP: Create Unique Variables In A For Loop

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();
}

Categories