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.
Related
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 1 year ago.
Improve this question
after sorting the $at1 array , iwant the other arrays keys to be sorted as $at1 array !
<?php
$p1 = array (0=>'p1',1=>'p2',2=>'p3',3=>'p4',4=>'p5');
$at1 = array (0=>0, 1=>4, 2=>7, 3=>6, 4=>2);
$cbt1 = array(0=>5,1=>1,2=>2,3=>2,4=>1);
asort($at1);
?>
Looks like homework :)
$p1 = array (0=>'p1',1=>'p2',2=>'p3',3=>'p4',4=>'p5');
$at1 = array (0=>0, 1=>4, 2=>7, 3=>6, 4=>2);
$cbt1 = array(0=>5,1=>1,2=>2,3=>2,4=>1);
asort($at1);
$a_keys = array_keys($at1);
$p1 = repeat_order($a_keys, $p1);
$cbt1 = repeat_order($a_keys, $cbt1);
function repeat_order( $mask=array(), $haystack)
{
$result = array();
foreach ( $mask as $key ):
if ( isset($haystack[$key]) ):
$result[$key] = $haystack[$key];
unset($haystack[$key]);
endif;
endforeach;
// If the array to be sorted had more elements
foreach ( $haystack as $k => $v ):
$result[$k] = $v;
endforeach;
return $result;
}
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;
?>
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>";
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 found problem with this:
$explode = explode($start, $data);
$abc = explode($end, $explode[1]);
$found = $abc[0] . '<br/>';
$found .= $abc[1] . '<br/>';
$found .= $abc[2] . '<br/>';
return $found;
The abc[0], abc[1] and more is randomly based by exploded results. How to define if $found is array or something I can loop it in foreach?
That's. Thank You for answer.
explode($start, $data) will return an array unless $start is an empty string "" in which case it will return false. You can confirm whether $found is an array by using is_array($found) which will return true if $found is an array and false otherwise.
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';
}
?>