Friday, 23 February 2018

Sorting Arrays In PHP

Array elements can be sorted in descending or ascending, alphabetical or numerical order.
Sort Functions
  • 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

Sort Array in Ascending Order - sort()
The following example sorts the elements of the $num array in ascending numerical order:
Example 1:
<?php
$num = array(44, 76, 24, 2, 115);
sort($num);
$len = count($num);
for($x = 0; $x < $len; $x++) {
    echo $num[$x];
    echo "<br>";
}
?>
Output:
2
24
44
76
115
Example 2:
<?php
$name = array("dev", "surya", "chandan");
sort($name);
$len = count($name);
for($x = 0; $x < $len; $x++) {
    echo $name[$x];
    echo "<br>";
}
?>
Output:
chandan
dev
surya

Sort Array in Descending Order - rsort()
The following example sorts the elements of the $num array in descending alphabetical order:
Example 1:
<?php
$num = array(44, 76, 24, 2, 115);
rsort($num);
 $len = count($num);
for($x = 0; $x < $len; $x++) {
    echo $num[$x];
    echo "<br>";
}
?>
Output:
115
76
44
24
2
 Example 2:
<?php
$name = array("dev", "surya", "chandan");
rsort($name);
$len = count($name);
for($x = 0; $x < $len; $x++) {
    echo $name[$x];
    echo "<br>";
}
?>
Output:
surya
dev
chandan

Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:
Example :
<?php
$id = array("dev"=>"45", "surya"=>"25", "gopal"=>"33");
asort($id);
foreach($id as $x => $x_demo) {
    echo "Key=" . $x . ", Value=" . $x_demo;
    echo "<br>";
}
?>
Output:
Key=surya, Value=25
Key=gopal, Value=33
Key=dev, Value=45


Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:
Example:
<?php
$id = array("dev"=>"45", "surya"=>"25", "gopal"=>"33");
ksort($id);
foreach($id as $x => $x_demo) {
    echo "Key=" . $x . ", Value=" . $x_demo;
    echo "<br>";
}
?>
Output:
Key=dev, Value=45
Key=gopal, Value=33
Key=surya, Value=25

Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to the value:
Example:
<?php
$id = array("dev"=>"45", "surya"=>"25", "gopal"=>"33");
arsort($id);

foreach($id as $x => $x_demo) {
    echo "Key=" . $x . ", Value=" . $x_demo;
    echo "<br>";
}
?>
Output:
Key=dev, Value=45
Key=gopal, Value=33
Key=surya, Value=25

 Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:
Example:
<?php
$id = array("dev"=>"45", "surya"=>"25", "gopal"=>"33");
krsort($id);
foreach($id as $x => $x_demo) {
    echo "Key=" . $x . ", Value=" . $x_demo;
    echo "<br>";
}
?>
Output:
Key=surya, Value=25
Key=gopal, Value=33
Key=dev, Value=45

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...