0

I have done a lot of research on google about it but I don't know how to achieve that in PHP. Actually I have an array with values and for each values I would like to get 5 new values incremented of 1.

Exemple :

$valueseg = array( 0 => 2510 , 1 => 1700); 

I would like to get this result :

$valueseg = array( 0 => 2510, 1 => 2511, 2 => 2512, 3 => 2513, 4 => 2514 , 5 => 2515, 6 => 1700, , 7 => 1701, , 8 => 1702, , 9 => 1703, 10 => 1704 , 11 => 1705); 

I tried to do that with a foreach and a for loop but I can't retrieve an array.

$tabasc = array();

foreach($valueseg as $key => $value){

    for($i = 0; $i < 5; $i++){
        $tabasc[$i] = $value + $i ;
    }
}

With an echo inside the for loop I have a result inline like this : 2478247924802481248225102511251225132514

And if I try to do a var_dump of $tabasc it shows me values incremented of 1 only for the first value of $valueseg.

Do you know how to do that ?

Thanks

1
  • Do $tabasc[] = $value + $i ; Then you won't overwrite the previous values. Commented Jul 8, 2017 at 7:32

1 Answer 1

4

Do it like below:-

<?php

$valueseg = array( 0 => 2510 , 1=> 1700); 

$final_array = array(); //create a new array

foreach($valueseg as $values){ // iterate over your original array
  for($i=1;$i<=5;$i++){ // loop 5 times
    $final_array[] = $values+$i; //add incremented $i value to original value and assign to new array
  }

}

print_r($final_array); // print new array

Output:- https://eval.in/829546

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.