Asked  1 Year ago    Answers:  5   Viewed   12 times

Ok so i have this php foreach loop

<?php foreach ($step_count->rows as $step) { ?>

and $step will be the step numbers 1, 2, 3, 4, 5 up to the total steps

within the loop i a need to set the value of the images within the loop to standard_image_1 or whatever step there is...so for example

<input value="<?php echo {$standard_image_"$step['number']"}; ?>" />

so basically i need the variable $standard_image_1 and so on depending on the steps but i dont know the correct syntax to do this

 Answers

5

Look at the docs for "variable variables" - http://php.net/manual/en/language.variables.variable.php

<?php echo ${'standard_image_'.$step['number']}; ?>

Here's a mock-up, using the details you've given: http://codepad.org/hQe56tEU

Thursday, April 1, 2021
5

That is very strange. You could try

$products[0][''][0]['product_name']
Thursday, April 1, 2021
 
Xavio
 
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))    
  }  
}
Friday, May 28, 2021
 
hnkk
 
3

This should do it just fine:

$newArray = array_map(function (array $item) {
    return $item[0]."some code".$item[1]."some code".$item[2]."some code";
}, $array);

var_dump($newArray);

I don't see where separate variables are needed at all.

If you just continuously number variables dynamically ($item1, $item2 etc.), you're trying to hold a dynamic number of elements. That's exactly what arrays are for: $items[0], $items[1] etc.

Saturday, May 29, 2021
 
Hilmi
 
4

Just de-denting your print() will work:

def square(x):
    guess = int(x/2)
    for i in range(1,10):
        nextguess = (guess + x/guess)/2
        guess=nextguess
    print(nextguess)

After the loop, nextguess still has the value form the last cycle. In Python, a loop does not create a new scope. So, everything you create or change in the loop is still available after the loop.

Thursday, August 5, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :