PHP - for loops through associative array - php

I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).
$_POST[Var1]
$_POST[Var2]
$_POST[Var3]
How do I use a for loop to loop through and echo the values in each?
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . '$i'];
}
This does not seem to work.

Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . $i];
}
This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.

Check this. While using php varibles dont put upper commas for the variables.
<?php
$_POST["file1"];
$_POST["file2"];
$_POST["file3"];
for ($i = 1; $i <= 3; $i++){
echo $_POST['file'.$i];
}

Or this way:
$_POST["Var{$i}"];

Related

PHP array element as variable [duplicate]

how do i create variable variables inside a for loop?
this is the loop:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
}
inside this loop i would like to create a variable $seat for each time it passes but it has to incrementlike so. first time it passes it should be $seat1 = $_POST['seat'+$aantalZitjesBestellen], next time it passes: $seat2 = $_POST['seat'+$aantalZitjesBestellen] and so on.
so at the end it should be:
$seat1 = $_POST['seat1'];
$seat2 = $_POST['seat2'];
and so on.
so the variable and the content of the $_POST should be dynamic.
Firstly, I would use an array for this unless I'm missing something. Having variables like $seat1, $seat2, etc tends to have far less utility and be far more cumbersome than using an array.
That being said, use this syntax:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$key = 'seat' . $counter;
$$key = $_POST[$key];
}
Lastly, PHP has an inbuilt function for extracting array keys into the symbol table: extract(). extract() has enormous potential security problems if you use it with unfiltered user input (eg $_POST) so use with caution.
This will work as well:
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
${'seat' . $counter} = $_POST['seat' . $counter];
}
(Expanded for clarity - you may be able to do a one-liner)
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$varname = 'seat' . $counter;
$$varname = $POST[$varname];
}
BUT! You really shouldn't do this. (And if you really must, see cletus' answer for the built-in PHP way to do it - this is considered bad practice too, though.)
Reconsider your problem and see if arrays might be the solution (I guess it will). This will make both inspection (via e.g. var_dump()) and iteration easier and does not pollute the global variable space.
for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
$name = 'seat' . $counter;
$$name = $_POST['seat' . $counter];
}
It's recommended to use arrays, as you can check them easier.
You can use extract but I don't recommended to do what you are trying to do.

Why does accessing an array element throw an array-as-string error in PHP?

I've done some searching and I didn't find any posts that quite answered my question.
I have a PHP array generated, for the sake of argument, with this code:
$i = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
When I try to access it with:
for($i = 0; $i < $j; $i++) {
echo "$multiArray[0][$i]";
echo "$multiArray[1][$i]";
}
I get:
Notice: Array to string conversion on line 3
Notice: Array to string conversion on line 4
...x4
When I replace echo with printf("%d", $multiArray[0][$i]) then it prints fine. Why do I have to explicitly tell PHP that I'm asking for an int when the element I'm accessing is clearly an int (and PHP knows it, via var_dump())? I'm not accessing the array, but an element within the array.
Thanks
Simple double quoted variable interpolation supports up to one nested element. In other words, "foo[0][1]" is interpreted as "{$foo[0]}[1]". That means it tries to interpret the array $foo[0] as a string at that point to interpolate it into the string.
But using quotes here at all is entirely nonsensical. You don't want string interpolation, you just want to output a variable value:
echo $multiArray[0][$i];
just try this:
for($i = 0; $i < $j; $i++) {
echo $multiArray[0][$i];
echo $multiArray[1][$i];
}
Your code is parsing the string not the array, try to remove the quotes.The [] brackets after the array are considered as a string not the as a paremeter.Use the code below
<?php
$j = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
for($i = 0; $i < $j; $i++) {
echo $multiArray[1][$i];
}
Hope this helps you

Making associative array

My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);

How to refer to a variable dynamically [duplicate]

I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...

PHP dynamically reference variable in string literal

I have multiple PHP variables in the form
$number1, $number2, $number3 and so on...
I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:
for($i = 1; $i <= 10; $i++) {
//The first number to be printed should be the value from
//$number1 not $number concatenated to $i
//here are some of the strings I tried:
echo "$number$i";
echo "{$number}$i";
echo "{$number}{$i}";
}
This should do it:
echo ${"number{$i}"};
But why not use arrays instead? Having $number[$i] is much more readable...

Categories