I have this array which stores the values in the array in this format
Array
(
[0] => 0,20
[1] => 21,50
[2] => 201,300
[3] => 301,400
)
now how will I find the smallest and the largest numeric value from it ?
I have this array which stores the values in the array in this format
Array
(
[0] => 0,20
[1] => 21,50
[2] => 201,300
[3] => 301,400
)
now how will I find the smallest and the largest numeric value from it ?
I think you need minimum and maximum range.
For minimum and maximum range, first walk through array and replace
,
by .
so that they become numbers (comparable).
Them find out min()
and max()
of the resulting array.
Find out the key where the elements sit.
Now, access the elements of the original array with these keys.
<?php
$org = $result = Array(
'0,20',
'21,50',
'201,300',
'301,400',
);
$result = array_map(function($el) {
return str_replace(',','.',$el); }, $result
);
echo '<pre>';
$minKey = array_search(min($result), $result);
$maxKey = array_search(max($result), $result);
$min2 = $org[$minKey]; // Returns 0,20
$temp = explode(',', $min2);
$min = $temp[0];
echo $min; // Prints 0
echo "<br/>";
$max2 = $org[$maxKey]; // Returns 301,400
$temp = explode(',', $max2);
$max = $temp[1];
echo $max; // Prints 400
?>