3v4l.org

run code in 300+ PHP versions simultaneously
<?php $PopulationOfTexas = array( 1999 => 20.56, // in millions 2000 => 21.56, 2001 => 22.56, 2002 => 23.56 ); //generate an array sohwing the difference in each year compared to the previous year $differneces = array(); $lastyear = null; foreach($PopulationOfTexas as $k=>$v){ if(empty($lastyear)){$lastyear = $k; continue;} $differneces[$k] = $k - $lastyear; $lastyear = $k; //use this later $lastitem = array("year"=>$k, "data"=>$v); } //print_r($differneces); //get the average difference per year $count = 0; $total = 0; foreach($differneces as $k=>$v){ $count++; $total += $v; } $average = number_format(($total/$count), 2); //print_r($average); //make a prediction $predictions = array(); for($i=0;$i<4;$i++){ $year = isset($year) ? $year+1 : $lastitem["year"]+1; $prediction = isset($prediction) ? $prediction+floatval($average) : $lastitem["data"]+floatval($average); $predictions[$year] = $prediction; } print_r($predictions);
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Array ( [2003] => 24.56 [2004] => 25.56 [2005] => 26.56 [2006] => 27.56 )

preferences:
224.29 ms | 404 KiB | 387 Q