Max and Min Value of array
The max()function returns the numerically highest of the parameter values. If multiple values can be considered of the same size, the one that is listed first will be returned.
When max() is given multiple arrays, the longest array is returned. If all the arrays have the same length, max() will use lexicographic ordering to find the return value.
When given a string it will be cast as an integer when comparing.
Example #1 Example uses of max():
---------------------------------
<?php
echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5
// When `hello` is cast as integer it will be 0. Both the parameters are equally
// long, so the order they are given in determines the result
echo max(0, `hello`); // 0
echo max(`hello`, 0); // hello
echo max(`42`, 3); // `42`
// Here 0 > -1, so `hello` is the return value.
echo max(-1, `hello`); // hello
// With multiple arrays of different lengths, max returns the longest
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)
// With multiple arrays of the same length, max compares from left to right
// using lexicographic order, so in our example: 2 == 2, but 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)
// If both an array and non-array are given, the array
// is always returned as it`s seen as the largest
$val = max(`string`, array(2, 5, 7), 42); // array(2, 5, 7)
?>
Min () PHP Function:
---------------------
The min () function returns the lowest number in a group or array. Any strings are seen as having a value of 0, and in the case that 0 is the lowest number the first case will be returned. You can also compare multiple arrays. This compares each individual array element, and whichever has a lower number first, is returned as the min. If an array is compared to a non array, the array is always seen as being the largest, and therefor never returned as the min.
Examples:
<?php
echo min(2, 4, 6, 8) ;
// Returns 2
echo min (array(2, 14, 7)) ;
//Returns 2
echo min(0, `about`) ;
//Returns 0
echo min (`about`, 0) ;
// Returns about
// Comparing two arrays
// so in our example: 5 = 5, but 7 < 9
$x = max(array(5, 7, 12), array(5, 9, 1)) ;
//Returns array(5, 7, 12)
// If both an array and non-array are given, the array is always seen as the largest, and therefor not returned
$x = max(`about`, array(2, 4, 6), 99) ;
//Returns about, as it is seen as 0
?>
Insert Item to a position
Let there is an array having five elements.
$array=array("1","2","3","4","5");
I want to add another element 'janaki' after position 3. So my array should be:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => janaki
[4] => 4
[5] => 5
)
So the script is:
<?php
$array=array("1","2","3","4","5");
$pos=3;
$val='janaki';
echo"<pre>";
print_r(array_insert($array,$pos,$val));
echo"</pre>";
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
?>