Increment values of an array using a for loop and a foreach

Thecheap

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

Serving Quarantine period

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Put values into array using foreach loop

How to get the array values using foreach loop

fetch values in array using angular.foreach to loop object values

Values of array changing without modification, using foreach loop

Change nested array values in multiple fields using foreach loop

increment the values of foreach within ifelse loop in C#

Increment array values evenly using javascript?

Use while loop to increment values in an array, untill all values are => 100

JavaScript foreach loop through an array of hex values, using setTimeout to loop through background colors

Loop through array of arrays and increment values into a new array

Display 2 Array values in foreach() loop - PHP

PHP foreach loop not reading values from array

Add values to an array inside a foreach loop

values from a foreach loop in a function into an array

Foreach loop specific subarray values in PHP array?

Exploding an array values within a foreach loop in PHP

Array within array using foreach loop in codeigniter

loop over BigInteger values using foreach in Kotlin

Using the values of a var inside a foreach loop

possibility to increment a duplicate with foreach loop

SwiftUI - Increment variable in a ForEach loop

I want to get specific values inside multiple Objects in One array using forEach or a loop in PHP

Entering values to an array using for loop

Add values to array using for loop

Increment value of array object by using values of another array's objects

Loop through an array of objects using forEach

How to loop array of objects using forEach?

Using foreach-loop to construct objects in the array

Displaying images from array using a foreach loop