Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds,
plz help me out to solve this below code works fine but wt i need is there is another var named as rollno the $b array is separated according to rollno for eg:500, 2000, 1500 belongs to roll no:1 and 750, 800 belongs to roll no:2 according to this the below process should done

$a = 230500;
$b = array(500, 2000, 1500, 1000, 750, 800, 900, 1300, 2000, 2500, 1400, 8000); //up to n number
foreach($b as $key){
$a = $a - $key;
$c = $a;
echo $c.'
';
}
Posted
Comments
Peter Leow 16-Jan-14 0:33am    
What and how the output should look like with the roll no?
Arun-23 16-Jan-14 3:12am    
o/p should look like for roll no:1 $b(500, 2000, 1500)for roll no:2 $b(1000, 750)like for all.. $B value is not constant for each rollno it may vary.. so the o/p comes like this rollno 1: 230000,228000,226500 and for rollno 2:229500,228750 like wise for all rollno.. note each rollno values should subtract with $a not on previous rollno value.
Peter Leow 16-Jan-14 3:52am    
Does every roll contains 3 values? such as:
roll no 1 : 500, 2000, 1500
roll no 2: 1000, 750, 800
and so on.
Arun-23 16-Jan-14 4:17am    
no it may vary some has 3 some has even 5 and even some has 10 too its not constant that's the pbm..
Peter Leow 16-Jan-14 4:30am    
No can do. Unless there are certain rules or formula to derive the number of elements for each roll.

1 solution

Since the number of elements is not fixed, let consider using random number.
PHP
<?php
$v = 230500;
$b = array(500, 2000, 1500, 1000, 750, 800, 900, 1300, 2000, 2500, 1400, 8000); //up to n number

// get the size of $b dynamically
$N = count($b);

// randomly determine the next n number in $b
$n = rand(1,$N);

$s = 0;

$rollno = 0;

while ($n <= $N){

    $display = 'roll no '.++$rollno.': ';
    $a = $v;

    for ($i = $s; $i < $n; $i++)
    {
        $a = $a - $b[$i];
        $display = $display.$a.', ';
    }

    $display = substr($display,0,-2).'<br>';

    echo $display;

    // randomly determine the next n number
    $s = $n;
    $n = $n + rand(1, $N - $n);

}

?>

The number of element in each roll is determined randomly. So, the outcome will be different for each run.
 
Share this answer
 
v2
Comments
Arun-23 23-Jan-14 1:20am    
hi Peter Leow thanks for your help..
Happy Coding :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900