It sorts an array by keys using a user-defined comparison function
Example
<?php
function func($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
echo “<pre>”;
$array = array(1, 7, 5, 8, 4);
usort($array, “func”);
print_r($array);
?>
Output
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 7
[4] => 8
)
It sorts an array by keys using a user-defined comparison function
Example
<?php
function func($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
echo “<pre>”;
$array = array(1=>”PHP”, 7=>”ASP”, 5=>”JS”, 8=>”AS”, 4=>”JSP”);
uksort($array, “func”);
print_r($array);
?>
Output
Array
(
[1] => PHP
[4] => JSP
[5] => JS
[7] => ASP
[8] => AS
)
It sorts an array with a user-defined comparison function and maintain index association
Example
<?php
echo “<pre>”;
$lan = array(“PHP”,”ASP”,”JSP”);
uasort($lan);
print_r($lan);
?>
Output
Array
(
[0] => PHP
[1] => ASP
[2] => JSP
)
It sorts the array
Example
<?php
echo “<pre>”;
$lan = array(“PHP”,”JSP”,”ASP”);
sort($lan);
print_r($lan);
?>
Output
Array
(
[0] => ASP
[1] => JSP
[2] => PHP
)
It an alias of count() and returns the number of elements
Example
<?php
$lan = array(“PHP”,”JSP”,”ASP”);
echo sizeof($lan);
?>
Output
3
It shuffles the array
Example
<?php
echo “<pre>”;
$lan = array(“PHP”,”JSP”,”ASP”);
shuffle($lan);
print_r($lan);
?>
Output
Array
(
[0] => ASP
[1] => PHP
[2] => JSP
)
It sorts the array in reverse order
Example
<?php
echo “<pre>”;
$lan = array(“PHP”,”JSP”,”ASP”);
rsort($lan);
print_r($lan);
?>
Output
Array
(
[0] => PHP
[1] => JSP
[2] => ASP
)
it set the internal pointer of an array to its first element
Example
<?php
$lan = array(“PHP”,”JSP”,”ASP”);
next($lan);
next($lan);
echo reset($lan);
?>
Output
PHP
It can be used to create an array containing a range of elements
Example
<?php
echo “<pre>”;
$array = range(3, 7);
print_r($array);
?>
Output
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
It rewinds the internal array pointer
Example
<?php
$lan = array(“PHP”,”JSP”,”ASP”);
next($lan);
echo prev($lan);
?>
Output
PHP
Zend | Magento Certified PHP | eCommerce Architect