PHP Sort Array:
Sometimes we need to store data in a single entity called Array, unlike in other language in PHP, an array can store values of different datatype.
In the following examples we will learn how to create array, associative array, how to sort values of an array, and how to sort the keys of an associative arrays are given.
Go through each example and you will come to know how to use array in PHP.
PHP Sort Array Example:
<?php
$array
=array("Hello","New","World");echo
count($array);sort(
$array);for
($i=0;$i<=4;$i++){ echo $array[$i]."<br/>";}
$array
=array("Hello","Hi","Hei");sort(
$array);for
($i=0;$i<=4;$i++){ echo $array[$i]."<br/>";}
$array
=array("Hello"=>1,"Hi"=>2,"Hei"=>3);asort(
$array);foreach
($array as $key=>$value){ echo "Key: ".$key."Value: ".$value."<br/>";}
echo
"<br/>";$array
=array("Hello"=>1,"Hi"=>2,"Hei"=>3);ksort(
$array);foreach
($array as $key=>$value){ echo "Key: ".$key."Value: ".$value."<br/>";}
?>
Output:
3Hello
New
World
Hei
Hello
Hi
Key: HelloValue: 1
Key: HiValue: 2
Key: HeiValue: 3
Key: HeiValue: 3
Key: HelloValue: 1
Key: HiValue: 2
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.