Dynamically name PHP variable in a Loop - php

I am having trouble dynamically naming my PHP variables when doing a foreach loop. Here is my code:
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member'][$i]['fname'];
$lname = $_POST['member'][$i]['lname'];
}
How would I append the number contained in $i to $fname & $lname so that $fname & $lname become $fname1 & $lname_2 and so on? Or is there a better way of doing this? Nothing I have tried works.

$i = 0;
$output = array();
foreach ($_POST['member'] as $member)
{
$output['fname' . $i] = $member['fname'];
$output['lname' . $i] = $member['lname'];
$i++;
}
extract($output);
Though..why?

EDIT: Answer to your new question - how to store in the session:
session_start();
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$_SESSION["fname_{$i}"] = $_POST['member'][$i]['fname'];
$_SESSION["lname_{$i}"] = $_POST['member'][$i]['lname'];
}

Try this
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname.'_'.$i = $_POST['member'][$i]['fname'];
$lname.'_'.$i = $_POST['member'][$i]['lname'];
}
So you can access $fname_1,$fname_2 etc...

Hi try the simple concatination(".") operator of php.. So your code will be
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member']['fname'.$i];
$lname = $_POST['member']['lname_'.$i];
}

Related

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

counting the number of items in an array

I have a string as shown bellow:
SIM types:Azadi|Validity:2 Nights|Expirable:yes
I have the following code to seperate them by | and then show them line by line
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'][] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
but the for loop is iterated only once and prints only the first value. This is what i am getting now:
SIM types:Azadi
Try like this
$others['items'] = explode("|",$other);
$my_count = count($others['items']);
for($i = 0; $i < $my_count; $i++){
echo $others['items'][$i];
}
Change
$others['items'][] = explode("|",$other);
to
$others['items'] = explode("|",$other);
remove []
Explode will return a array. ref: http://php.net/manual/en/function.explode.php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others['items'] = explode("|",$other);
for($i = 0; $i < count($others['items']); $i++){
echo $others['items'][$i];
}
Try this:
<?php
$other = "SIM types:Azadi|Validity:2 Nights|Expirable:yes";
$others = explode("|",$other);
$total = count($others);
for($i = 0; $i < $total; $i++){
echo $others[$i];
}
?>

How do I simplify this basic loop?

I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.
use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}
I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>

create var's with others

I want to create somthing like 100 var's which their names will be:
$numbr_1 = 1;
$numbe_2 = 2;
$number_3 = 3;
...
I won't write 100 vars of course, but there is a way to do it with foor loop or somthing? I thought about this:
for ($i = 1; $i <= 100; $i++)
$number_{$i} = $i;
You're talking about variable variables, and they are incredibly stupid to use. For one, they make debugging next to impossible.
What you want is an array:
for ($i = 1; $i <= 100; $i++) {
$numbers[$i] = $i;
}
Something like this should work:
for($i = 1 ; $i <= 100 ; $i++){
$var_name = "number_$i";
$$var_name = $i;
}
for($i=1;$i<=100;$i++) {
$j="number$i";
$$j = $i;
}
Why don't you use an array?
$number = array();
for ($i = 0; $i < 100; $i++)
{
$number[] = $i;
}
for($i = 1 ; $i <= 100 ; $i++){
${'number_'.$i} = $i;
}
Possible solution is usage of array.
$number = array();
for ($i = 1; $i <= 100; $i++) {
$number[$i] = $i;
}

How to get the previous value from incrementing variable in for loop?

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?

Categories