Ordering of PHP arrays

Source: Internet
Author: User

In PHP, you can use the sort () function to sort the array elements.

Cases:

<?php
$arr =array (75,32,65,98,85,12);
Sort ($arr);
foreach ($arr as $k => $var) {
echo $var. " &nbsp; ";
}
?>

You can also sort the array elements using classic algorithms like bubble sort, quick sort, and the following are examples of bubble ordering:

Bubble sort

PHP File:

<?php
With bubble sort method, the small to large arrangement
$arr =array (75,32,65,98,85,12);
$temp = 0;
Why is Count ($arr)-1? For example, 3 digits, the location of the two number of digits, the 3rd nature is determined.
For ($i =0 $i <count ($arr)-1; $i + +) {
For ($j =0 $j <count ($arr)-$i; $j + +) {
if ($arr [$j]> $arr [$j +1]) {
$temp = $arr [$j];
$arr [$j]= $arr [$j +1];
$arr [$j +1]= $temp;
}
}
}
foreach ($arr as $k => $var) {
echo $var. " &nbsp; ";
}
?>

The results of the operation are shown below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.