Viewed   53 times

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.

 Answers

4

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.

Saturday, September 17, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

[email protected]:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
4

Consider using get() to obtain environment variables from string values. Additionally, consider the nested lapply() between dataframe and model lists for more organized returned object. Nested for loops would require appending each iteration into growing list unless you just need to output. Below examples use linear models, lm():

model1 <- y ~ x1
model2 <- y ~ x2
model3 <- y ~ x3

dflist <- c("df1","df2","df3")
modelist <- c("model1", "model2", "model3")

# MODEL DATA RETURNS NESTED LIST OF 3 ELEMENTS 
# EACH WITH CORRESPONDING DATA METRICS (COEFF, RESIDUALS, ETC.)
modeldata <- lapply(dflist,
                    function(x) {                  
                    df<-get(x)       
                    lapply(modelist,
                           function(y) {
                           model <- get(y)
                           ols <- lm(model, df)                                          
                    })                  
               })

# BELOW CREATES MODEL SUMMARY LIST OF 3 ELEMENTS 
# FOR FIRST THREE MODELS USING FIRST DATASET
modelsummary <- lapply(modeldata[[1]], summary)

Example with nested for loop:

# INITIALIZE A LIST TO CONTAIN DATA
modeldata <- list()

for (i in dflist){  
  df<-get(i)
  for (j in modelist){    
    model <- get(j)

    # APPEND TO MODELDATA LIST
    # FINAL RETURN IS LARGE LIST OF ALL DATA MODELS
    modeldata <- c(modeldata, lm(model, df))    
  }  
}
Sunday, September 11, 2022
 
5

Python doesn't have a feature that's directly equivalent to PHP's "variable variables". To get a "variable variable"'s value (or the value of any other expression) you can use the eval function.

foo = "Hello World"
print eval("foo")

However, this can't be used in an import statement.

It is possible to use the __import__ function to import using a variable.

package = "os"
name = "path"

imported = getattr(__import__(package, fromlist=[name]), name)

is equivalent to

from os import path as imported
Thursday, September 8, 2022
 
baraa
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :