Jump to content

splitting arrays and calculating


Slowie

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/261784-splitting-arrays-and-calculating/
Share on other sites

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>';
?>

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        )

 

}

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

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.