PHP combine rows - php

I am working on a PHP project in which I need to combine rows (the rule is: if the first two numbers matched then add the rest numbers)
I have this array :
array_b4_combine= [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
the output should be :
combined= [
[2,15,5,5,0],
[2,21,2,2,0],
[2,24,9,9,0],
[3,15,8,8,0],
[3,24,1,1,0]];
This is my code :
$num1 = $array_b4_combine[0][0];
$num2 = $array_b4_combine[0][1];
$sum1 = 0;
$sum2 = 0 ;
$sum3 = 0 ;
$combined ;
for ( $i = 0 ; $i < count($array_b4_combine) ; $i++)
{
if ($num1 == $array_b4_combine[$i+1][0] && $num2 == $array_b4_combine[$i+1][1])
{
$sum1 = $sum1 + $array_b4_combine[$i][2];
$sum2 = $sum2 + $array_b4_combine[$i][3];
$sum3 = $sum3 + $array_b4_combine[$i][4];
}
else
{
$combined[] = array($num1 , $num2 , $sum1, $sum2, $sum3);
$day = $array_b4_combine[$i][0];
$time = $array_b4_combine[$i][1];
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
}
the output for my code is this:
combined=
[[2,15,4,4,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0]];
Am I doing the reset clause in wrong order.. can someone figure out what is the problem here.
Thanks

I really cannot understand your code, try this one:
$combined = [];
foreach ($array_b4_combine as $i => $arrayRow) {
$k = $arrayRow[0].' '.$arrayRow[1];
if (isset($combined[$k]))
for ($i=2; $i<5; $i++)
$combined[$k][$i] += $arrayRow[$i];
else
$combined[$k] = $arrayRow;
}
$combined = array_values($combined);

Here is how i solved your problem . code can be optimized .
<?php
$array_b4_combine = [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
$j =0;
for ( $i = 0 ; $i < count($array_b4_combine) -1 ; $i++)
{
if($i == 0)
{
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
if(($array_b4_combine[$i][0] == $array_b4_combine[$i+1][0]) && ($array_b4_combine[$i][1] == $array_b4_combine[$i+1][1]) )
{
$sum1 = $sum1 + $array_b4_combine[$i+1][2];
$sum2 = $sum2 + $array_b4_combine[$i+1][3];
$sum3 = $sum3 + $array_b4_combine[$i+1][4];
$combined[$j][0] = $array_b4_combine[$i][0];
$combined[$j][1] = $array_b4_combine[$i][1];
$combined[$j][2] = $sum1;
$combined[$j][3] = $sum2;
$combined[$j][4] = $sum3;
}
else
{ $j++;
$combined[$j][0] = $array_b4_combine[$i+1][0];
$combined[$j][1] = $array_b4_combine[$i+1][1];
$combined[$j][2] = $array_b4_combine[$i+1][2];
$combined[$j][3] = $array_b4_combine[$i+1][3];
$combined[$j][4] = $array_b4_combine[$i+1][4];
$sum1 = $combined[$j][2];
$sum2 = $combined[$j][3];
$sum3 = $combined[$j][4];
}
}
echo "<pre>";
print_r($combined);
echo "</pre>";
?>

Related

How to show the next interest rate like image expected below

Expectation
My Code
<?php
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = round($value * (pow(1+6/100, $i)), 2);
echo $i, " ", $income, "<br>";
}
?>
Output
1 106
2 112.36
3 119.1
4 126.25
5 133.82
How can I get result like my expectation above ?
Just add one more loop
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = array();
for ($j = 6; $j <= 10; $j++) {
$income[] = round($value * (pow(1+$j/100, $i)), 2);
}
echo $i, " ", implode(' ', $income), "<br>";
}

How to get result in three columns?

This is my code....
<?php
$arr1 = array();
$arr2 = array();
$arr3 = array();
$intial_amount = 1000;
$sum = 0;
for($j = 3; $j<=24; $j = $j + 3)
{
if($j == 3)
{
$arr1[$j] = "";
$sum = $sum + 1000*3;
}
else
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr1[$j] = $sum;
}
}
for($j = 27; $j<=48; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr2[$j] = $sum;
}
for($j = 51; $j<=72; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr3[$j] = $sum;
}
$arr = array($arr1,$arr2,$arr3);
foreach($arr as $key => $val)
{
foreach($val as $k => $v)
{
echo $k." ".$v."<br>";
}
echo "<br>";
}
?>
i want the result in three columns..
I'm not following your code so well, as it's unclear what you're doing, but if you want to display a list in three columns, use CSS's columns:
<html>
<ul style="columns: 3; -webkit-columns: 3; -moz-columns: 3;">
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
<li>something</li>
</ul>
</html>
I think you want something like that
if you do use the following code:
$arr1 = array();
$arr2 = array();
$arr3 = array();
$intial_amount = 1000;
$sum = 0;
for($j = 3; $j<=24; $j = $j + 3)
{
if($j == 3)
{
$arr1[$j] = "";
$sum = $sum + 1000*3;
}
else
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr1[$j] = $sum;
}
}
for($j = 27; $j<=48; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr2[$j] = $sum;
}
for($j = 51; $j<=72; $j = $j + 3)
{
$sum = $sum + 1000*3;
$sum = $sum + ($sum * 0.02);
$arr3[$j] = $sum;
}
$arr = array($arr1,$arr2,$arr3);
foreach($arr as $key => $val)
{
echo '<div style=" width:33%; float:left">';
foreach($val as $k => $v)
{
echo $k." ".$v."<br>";
}
echo '</div>';
}

algorithm for create blog page via for or foreach

I try create a simple system blog page with this algorithm:
<?php
$page = floor(10 / 10);
$limit = 40;
$num= 100;
$page = 1;
for ($record = 0; $record <= $num ; ++$record )
{
if($record % $limit == 0)
{
if(!($record < $limit))
{
echo 'page:'.$page.'<br/>';
for($id = $record - $limit +1 ; $id <= $record ; ++$id)
{
echo $id.'<br/>';
}
$page ++;
echo '<hr>';
}
}
}
?>
but it seems do not work for example... on $limit=40 I lost last 20 id!
can help me to improve this algorithm or suggest me a better way>
<?php
$limit = 40;
$items = 100;
$page_counter = 1;
$excess_items = $items % $limit;
if($excess_items > 0)
$total_page = ($items / $limit) + 1;
$current_count = 1;
while($page_counter <= $total_page){
echo 'total_page:'.$page_counter.'<br/>';
$cutting_count = 1;
while($cutting_count <= $limit){
if($current_count <= $items)
echo $current_count.'<br/>';
$cutting_count ++;
$current_count ++;
}
echo '<hr>';
$page_counter++;
}
?>

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

Sum the value of variable return by for loop in php

I have this code that is working perfectly. My question is how can I sum all the value of $manpowerCost and $otherCharges?
for ($i = 1; $i<=9; $i++) {
$m = '0'.$i;
$sumMc = array();
$revenue = revenue($project, $year.'-'.$m);
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$otherCharges = otherCharges($project, $year.'-'.$m);
$netIncome = netIncome($revenue, $manpowerCost, $otherCharges);
if ($manpowerCost<=0 && $m <= $projected_date){
$manpowerCost = round(($projected_mc * $percentage_mc),2);
$projected_total_mc += $manpowerCost;
$pt = $pt + $manpowerCost;
} else {
$projected_mc = $manpowerCost;
$manpowerCost = $projected_mc;
}
if ($otherCharges<=0 && $m <= $projected_date){
$otherCharges = round(($projected_oc * $percentage_oc),2);
$projected_total_oc += $otherCharges;
$pt = $pt + $otherCharges;
} else {
$projected_oc = $otherCharges;
$otherCharges = $projected_oc;
}
echo '<td>RE:'.number_format($revenue, 2, '.', '').'<br />MC:'.number_format($manpowerCost, 2, '.', '').'<br />OC:'.number_format($otherCharges, 2, '.', '').'<br />NI:'.$netIncome.'</td>';
}
U can Try like this
$powercost_total = '0';
for ($i = 1; $i<=9; $i++) {
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$powercost_total = $manpowerCost + 4powercost_total;
....
Declare $TotalManPowerCost and $TotalOtherCharges variables outside of the loop, and add $manpowercost and $otherCharges to them in each iteration of the loop

Categories