PHP Changing the index of a variable

$ string [$ k] = $ function [$ k]

defined in a foreach loop with index $ k. I want $ string to be defined as

$string[$k] = $function[$(k-5)]

      

except that it is not true. So for $ k = 8 I would have

$string[8] = $function[3]

      

How can I achieve this?

thank

+2


source to share


3 answers


Your version:

$string[$k] = $function[$(k-5)]

      



Correct version:

$string[$k] = $function[$k-5]

      

+2


source


Try the following:

$string[$k] = $function[$k-5];

      



(Andrew Hare suggested this already, but for some reason he deleted it.)

0


source


Question: Is $ function an array [] or a function call ()?

If $ function is an array, you should not refer to $ function [k-5], where k <5;

your for loop should read:

for ($k=5; $k<$limit; ++k)
    $string[$k] = $function[$k-5];

      

0


source







All Articles