I have the following array:
Array ( [0] => 80 ) Array ( [0] => 20 ) Array ( [0] => 90 )
Code:
$percentage_query = $this->Common_model->getTableData('prop_payment_percentage', array('list_id' => $id));
$percentage_result = $percentage_query->result_array();
foreach ($percentage_result as $ptime) {
$percentage_start_date = $percentage_query->row()->start_date;
$percentage_end_date = $percentage_query->row()->end_date;
$percentage_percentage = $percentage_query->row()->percentage;
//Days between start date and end date -> seasonal price
$start_time = $ptime['start_date'];
$end_time = $ptime['end_date'];
$hightest_percentage = $ptime['percentage']; // this is the array 802090
//help me echo 90
}
How should i return the Value 90? As It is the highest number which i would like to return into a $variable
The easiest way would be to use the standard code function from php:
$array = array(0 => 80, 1 => 20, 2 => 90);
$variable = max($array);
See also the documentation at http://php.net/manual/en/function.max.php
update: this function takes more than just an array of integers. All these are valid and return 3
as expected:
$a1 = array(1);
$a2 = array(2);
$a3 = array(3);
$i1 = 1;
$i2 = 2;
$i3 = 3;
$arr1 = array($a1, $a2, $a3);
$arr2 = array($i1, $i2, $i3);
echo max($a1, $a2, $a3), "
"; // 3
echo max($i1, $i2, $i3), "
"; // 3
echo max($arr1), "
"; // 3
echo max($arr2), "
"; // 3
It does not matter what keys are used in the arrays.
update 2: if you really need to work from the value 802090
and you can be sure that the values are always 2-digit percentages, you could also use the str_split
function (and then again the max
function):
$hightest_percentage = 802090;
$hpr = str_split($hightest_percentage, 2); // array ( 80, 20, 90 );
print_r(max($hpr)); // 90
if you have an array containing your arrays, you can do it like this:
$masterArray = array(
array(80),
array(20),
array(90),
);
$highestValue = 0;
foreach($masterArray as $arr){
$highestValue = $arr[0] > $highestValue ? $arr[0] : $highestValue;
}
echo $highestValue; //echo 90;
Here is the example If you have 3 array or variables.
$a1 = Array ( "0" => 80 );
$a2 = Array ( "0" => 20 );
$a3 = Array ( "0" => 90 );
$a4 = array_merge($a1,$a2);
$a5 = array_merge($a4,$a3);
echo '<pre>';
print_r(max($a5));
You don't need to loop. Merge the three arrays and then use max();
$array = array_merge(array(80),array(20),array(20));
$max = max($array);
Unless the array is sorted, that's the best you're going to get. If it is sorted, just take the first and last elements.
Of course, if it's not sorted, then sorting first and grabbing the first and last is guaranteed to be less efficient than just looping through once. Even the best sorting algorithms have to look at each element more than once (an average of O(log N) times for each element. That's O(N*Log N) total. A simple scan once through is only O(N).
If you are wanting quick access to the largest element in a data structure, take a look at heaps for an efficient way to keep objects in some sort of order.
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
if we use ascending order then to access last element we have to count array size and arr[last_position] gives highest order
But by sorting array in descending order there is no need to count array size. Just access first number and you will get highest number
<?
$a=array(0 => 1300 );
$b=array(0 => 500 );
$c=array(0 => 1220 );
$MaxValue=0;
if($a[0]>$b[0])
{
$MaxValue=$a[0];
if($b[0]>$MaxValue)
{
$MaxValue=$b[0];
}
else
{
if($c[0]>$MaxValue)
{$MaxValue=$c[0];}
}
}
else
{
if($b[0]>$c[0])
{
$MaxValue=$b[0];
}
else
{
$MaxValue=$c[0];
}
}
echo $MaxValue;
?>