Slowie Posted April 29, 2012 Share Posted April 29, 2012 hey guys just a quick onei having a blank moment i have an array at themoment pulling from a database. i basically want to split this array based on a min and max value. divide it equally and then give it a value on each smaller array and where each array starts... to make it clearer - say i have an array with 600 values in i want it split up into 8 smaller arrays link so Array ( [0] => Array ( [start] => 0 [split] => 75 ) [1] => Array ( [start] => 75 [split] => 75 ) [2] => Array ( [start] => 150 [split] => 75 ) carrying on till the end ) how would i do this. Quote Link to comment https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/ Share on other sites More sharing options...
Drummin Posted April 29, 2012 Share Posted April 29, 2012 This should give you something to play with. <?php //Assumming $values is an array of numbers //Sample $values=range(20,542,3); //get min and max $max = max($values); $min = min($values); //Could attempt to find an even amount //count records $count = count($values); //Set minimum number of splits $i=2; while($count%$i!=0 && $i<=$count){ $i++; } //Set a default backup even if not even results if ($i==$count){ $step=15; }else{ $step=$count/$i; } //Build new array $newvalues=array(); foreach(range($min,$max,$step) as $value){ $newvalues[]=array('start'=>$value,'split'=>$step); } //show results echo '<pre>'; print_r($newvalues); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/#findComment-1341498 Share on other sites More sharing options...
Slowie Posted April 29, 2012 Author Share Posted April 29, 2012 Thank you however i changed my code overnight and now its more like this... i need it to divide up 800 into equal or near equal arrays. array(800 values) minarraysize = user submitted max array size = minarraysize+(minarraysize-1) remainder_after_division = count(array) % minarraysize ; arrays to split into = array / min array size if ((remainder_after_division < minarraysize ) || (remainder_after_division != 0)){ its here where i get stuck as if the arrays to split into is 25 i need it to cycle through 26 arrays adding 1 each time untill all of the values have been used so itd look like => Array ( [start] => 0 [split] => 36 ) [1] => Array ( [start] => 36 [split] => 36 ) [2] => Array ( [start] => 72 [split] => 35 ) [3] => Array ( [start] => 107 [split] => 35 ) } Quote Link to comment https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/#findComment-1341509 Share on other sites More sharing options...
Slowie Posted April 29, 2012 Author Share Posted April 29, 2012 Ive sorted this problem mind lock released this morning now i have an issue of i currently have and array what looks like this when output Array ( [0] => Array ( [start] => 58 ) [1] => Array ( [start] => 58 ) [2] => Array ( [start] => 57 ) [3] => Array ( [start] => 57 ) ) how would I now using php add the values from this array using only the previous and previous previous keys e.g 0 result would be 0 1 would be 58 2 would be 116 3 would be 173 and so on for a dynamic array thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/#findComment-1341515 Share on other sites More sharing options...
MMDE Posted April 29, 2012 Share Posted April 29, 2012 Ive sorted this problem mind lock released this morning now i have an issue of i currently have and array what looks like this when output Array ( [0] => Array ( [start] => 58 ) [1] => Array ( [start] => 58 ) [2] => Array ( [start] => 57 ) [3] => Array ( [start] => 57 ) ) how would I now using php add the values from this array using only the previous and previous previous keys e.g 0 result would be 0 1 would be 58 2 would be 116 3 would be 173 and so on for a dynamic array thanks in advance Please start to use the code tags.... Are they always numbered like 0..x? $total = 0; $array_count = count($array); for($i=0; $i<$array_count; $i++){ $total += $array[$i]['start']; } echo $total; if not: $total = 0; foreach($array AS $value){ $total += $value['start']; } echo $total; Quote Link to comment https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/#findComment-1341518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.