Functions for ascending, descending, and reordering of array elements in PHP

Source: Internet
Author: User
Tags foreach arrays range shuffle sort
In the PHP array learning excerpt section, we learned the basic PHP array and the display of the elements. Need to learn more about the operation of the PHP array. The first contact is the ascending, descending sort problem of the array element.

1, quickly create an array of functions range ()

Like what range () functionYou can quickly create an array of numbers from 1 to 9:


<?php
$numbers =range (1,9);
echo $numbers [1];
?>


Of course, using range (9,1) creates a 9 to 1 array of numbers. At the same time, range () can also create an array of characters from A to Z:


<?php
$numbers =range (a,z);
foreach ($numbers as $mychrs)
echo $mychrs. " ";
?>


Note case when using a character array, such as range (A,Z) and range (A,Z) are different.

The range () function also has third parameter, the function of this parameter is Set Step size, such as range (1, 9, 3The array elements created are: 1, 4, 7

ordering of regular arrays in 2,php

The elements in a generic array are represented by characters or numbers, so you can arrange the arrays in ascending order, a function that is sort ()。 Like what:


<?php
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
foreach ($people as $mychrs)
echo $mychrs. " ";
Sort ($people);
echo "<br/>---sorted---<br/>";
foreach ($people as $mychrs)
echo $mychrs. " ";
?>


The array elements that are sorted in ascending order are displayed as birth name Nation sex, and of course the sort () function is case-sensitive (letters from large to small) in the order that a ... Z...A...Z)

The Sort () function also has a second argument that shows whether ascending rules are used to compare numbers or strings. Like what:


<?php
echo "---sorted by number in ascending order---<br/>";
$num 2=array (' 26 ', ' 3 ',);
Sort ($num 2,sort_numeric);
foreach ($num 2 as $mychrs)
echo $mychrs. " ";

echo "<br/>---sorted by word Fu Shen order---<br/>";
$num 3=array (' 26 ', ' 3 ');
Sort ($num 3,sort_string);
foreach ($num 3 as $mychrs)
echo $mychrs. " ";
?>


Sort_numeric and sort_string are used to declare ascending order by number or character. If you are in ascending order of numbers: 3, 26, but if you follow the word Fu Shen order is: 26, 3.

In addition to ascending functions in PHP, there are Descending or backward-aligned functionsIt is Rsort () functionLike what:


$num 1=range (1,9);
Rsort ($num 1);


This is actually equivalent to range (9,1)

ordering of associative arrays in 3,php

In addition to supporting numeric indexed arrays, PHP supports related arrays. For example, the following array is a related (associative) arrays


$peoples =array (' xm ' => ' name ', ' xb ' => ' sex ', ' mz ' => ' nation ', ' cs ' => ' birth ');


Using sort ($peoples) defaults to sort by the ascending order of the element definition values, you can use the Asort () function in associative arrays to indicate that the most important of associative arrays is to sort by keywords (such as XM, XB, MZ, etc.) in ascending order, which is the function ksort () function.


<?php
$peoples =array (' xm ' => ' name ', ' xb ' => ' sex ', ' mz ' => ' nation ', ' cs ' => ' birth ');
foreach ($peoples as $mychrs)
echo $mychrs. " ";

echo "<br/>--sorted by element value in ascending order--<br/>";
Asort ($peoples);
foreach ($peoples as $mychrs)
echo $mychrs. " ";

echo "<br/>--in ascending order of keywords--<br/>";
Ksort ($peoples);
foreach ($peoples as $mychrs)
echo $mychrs. " ";
?>


And the regular array has the reverse sort rsort () descending function of the sort () ascending function, the associative array also has the corresponding descending function: the Asort () function and the Arsort () function, the Ksort () function, and the Krsort () function.

Memory: The prototype function is sort () where a, k indicates that associative array correlation must be predecessor, and the reverse sort uses the R modifier.

random ordering of 4,php array elements

Used in PHP Shuffle () functionRandomly reorder the elements of an array, such as:


<?php
$fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
Shuffle ($fer);

foreach ($fer as $mychrs)
echo $mychrs. " ";
?>


Different sorting combinations are displayed each time

5,php Array Reverse sort in original order

PHP can be used in array_reverse () functionReverses the array elements in the original order. Like what:


<?php
$fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
foreach ($fer as $mychrs)
echo $mychrs. " ";

$fer =array_reverse ($fer);
echo "<br/>--reverse--<br/> in original order";
foreach ($fer as $mychrs)
echo $mychrs. " ";
?>


Cnbruce cnrose cnjames Cnanne
--Reverse in original order--
Cnanne cnjames Cnrose Cnbruce

Watch this. $fer =array_reverse ($fer);Any sort function that precedes this is just declaring a reference and does not redefine the original array, but it needs to be redefined when I debug the function. This is because Array_reverse () is a modified copy of the original array, if you do not need an existing array, you can redefine the original array to achieve the purpose of overwriting, otherwise you will define an array save copy, such as: $fer _bak=array_reverse ($fer);

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.