How To Sum with Foreach php codeigniter [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a problem to sum the values ​​using foreach.
I want to add the value of 12000 to 11000 but the result is only 11000, which is the last data.
$no = 1;
foreach ($record->result_array() as $calc) {
$sum = 0;
$sum += $calc['harga_jual'] - $calc['diskon'];
$no++;
}

The initialization of variable $sum ...is meant to be outside the foreach loop ...
That is $sum = 0 is meant to be outside the foreach loop ...

Note:- The declaration & initialization of $sum = 0; is outside the foreach loop.
<?php
$sum = 0;
foreach ($record->result_array() as $calc)
{
$sum += $calc['harga_jual'] - $calc['diskon'];
}
echo $sum;
?>

Related

split string by order like 3x2x1 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
(PHP) how can I split a string in multiple strings separated by comma,
like is I have a string "Hello my neighbor",
then after the split process, it should be
("Hello my neighbor","Hello my","Hello neighbor","my neighbor","Hello","my","neighbor")
try this :
<?php
$string = "Hello my neighbor";
$str = explode(" ",$string);
$newArray = [];
$newArray[] = $string;
for($i = 0; $i<count($str); $i++) {
if($i <= 1) {
$newArray[] = $str[$i]." ".$str[$i+1];
if($i != 0) {
$newArray[] = $str[$i-1]." ".$str[$i+1];
}
}
$newArray[] = $str[$i];
}
echo "<pre>";
print_r($newArray);
echo "</pre>";

foreach - return the highest value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a foreach loop in php.
Loop will return 4 diffirent values and i want to display the highest from it.
Specifically, my loop will return array with date and temperature for that day.
The example code for indication:
foreach ($variable as $key => $value) {
$temperature = temprature();
$date = date();
$teploty[$date] = $teplota;
if(!isset($teploty[$date]) > -50) {
$teploty[$date] = $teplota;
}
}
Your code is confusing. This is how you find the highest value in an array of numbers:
$highest = null;
foreach ($numbers as $num) {
if (is_null($highest) || $num > $highest) {
$highest = $num;
}
}
You should be able to adapt this pattern to your code and data.

How to Adding values of two dimnetional array in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the below given array
Array
(
[0] => 2,3
[1] => 2,3
)
I want to sum up and get result in a variable. E.g. variable a = 4 (2+2=4) and variable b = 6 (3+3=6). I am coding in php.
Use EXPLODE inside foreach
<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$tempnew = explode(",",$temp);
$a += $tempnew[0];
$b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>
Try this
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
$a += $temp[0];
$b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
Output
a = 4
b = 6

Echo array just give the output array instead of value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This loop just gives me the output Array instead of the value that shows when I use the print_r function.
print_r gives me this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
)
but the echo just array
for($i=0; $i<($n*$n); $i++){
for($j=0; $j<($n*$n); $j++){
$number = "column" . $i . $j;
$plan = $field[$i][$j] = $_POST[$number];
$myvariable[] = $field[$i][$j];
}
echo $myvariable;
}
but if I remove the [] from $myvariable it prints out the values. the problem is that I need to use the array with an unique array
$unique = array_unique($myvariable);
if (count($unique) != count($myvariable)) {
echo ="no uniques";
}
Any tips?
You mean this ?
$unique = array_unique($myvariable);
foreach($unique as $value)
{
echo $value;
}
or make use of a typical for
$unique = array_unique($myvariable);
for($i=0;$i<count($unique);$i++)
{
echo $unique[$i];
}

How to Combine the two array's when the checkbox is checked [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two array and two check boxes. The array values are
$a={1,2,3,4,5} --->It' for first Check box
$b={5,6,7,8,9} --->It' for Second Check box
When I check the first check box only I want the result is
$c[0]=1,$c[1]=2,$c[2]=3,$c[3]=4,$c[4]=5
And I check the second check box only I want the result is
$c[0]=5,$c[1]=6,$c[2]=7,$c[3]=8,$c[4]=9
And I have check both check boxes I want the result is
$c[0]={1+5},$c[1]={2+6},$c[2]={3+7},$c[3]={4+8},$c[4]={5+9}
It's Possible?
You can achieve this using a for() loop. Please note that this code assumes both arrays are the same size:
$c = array();
for($i = 0; $i < count($a); $i++)
{
$c[] = ($a[$i] + $b[$i]);
}
print_r($c);
Yes you can do it following way
<?php
$a = array(1,2,3,4,5);
$b = array(5,6,7,8,9);
$c = array(); //for result
if(isset($_POST['checkbox_1'])){
$c = $a;
}
else if(isset($_POST['checkbox_2'])){
$c = $b;
} else if(isset($_POST['checkbox_1']) && isset($_POST['checkbox_2'])){
if(count($a) == count($b)){
for($i=0;$i<count($a);$i+}){
$c[]=$a[i] + $b[i];
}
}
}else{
echo 'Error Contact Admin';
}
?>

Categories