PHP array read out succession -
in php have array this:
$bulkarray[$i]
this array feed plenty of numbers (e.g. 1 => 100, 2 => 300, 3 => 100, , on). want find within whole range of numbers, greatest succession of number equal or less 500. want write number of succession.
for illustration have array
1 => 100, 2 => 300, 3 => 100, 4 => 50, 5 => 50, 6 => 50, 7 => 50, 8 => 50, 9 => 500, 10 => 200, 11 => 100
as can see, number 1,2 & 3 500. first succession.
2,3,4,5 500 together. (this succession(4) bigger first succession(3))
and on,then highest succession: 3,4,5,6,7,8 (succession 6 numbers) 350 (but lower 500m, searched it)
now, how can write array: $bulkarray[$i]
, highest succession 6 ?
because 6 highest succession found in whole array 500!
(it's categorizing specific carparts)
click here or here can see somehing
class="snippet-code-js lang-js prettyprint-override"><?php //lets phone call $bulkarray = $values; $values=array( 1 => 100, 2 => 300, 3 => 100, 4 => 50, 5 => 50, 6 => 50, 7 => 50, 8 => 50, 9 => 500, 10 => 200, 11 => 100 ); $sum=0; // sum until 500 $vals500=array(); // array store index <= 500 $i=0; // index new aaray store keys made <= 500 $key1=1; //value iterate array defined above //print_r($values); echo '<br/>'; for($key=1; $key <= count($values); $key++){ $sum = $sum + $values[$key]; // sum of values if($sum <= 500){ $vals500[$i][]=$key; } //append key of sum ==500 if($sum == 500) { $i++; $key=$key1++; $sum=0; } //check sum reinitialize values } foreach($vals500 $val){} //search sec array many indeces in $vals500. nb: fail place echo print_r($vals500); print 'the highest succession found '; print_r($vals500[2]); // ?>
php arrays
No comments:
Post a Comment