Home > Backend Development > PHP Tutorial > Functions to parse ascending, descending and reordering of array elements in PHP_PHP Tutorial

Functions to parse ascending, descending and reordering of array elements in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:04:40
Original
1265 people have browsed it

1, the function range() that quickly creates an array
For example, the range() function can quickly create an array of numbers from 1 to 9:

Copy code The code is as follows:

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

Of course, using range(9,1) creates a number array from 9 to 1. At the same time, range() can also create a character array from a to z:
Copy code The code is as follows:

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

Pay attention to the case when using character arrays. For example, range(A,z) and range(a,Z) are different.
The range() function also has a third parameter, which is used to set the step size. For example, the array elements created by range(1,9,3) are: 1, 4, 7

2. Sorting of conventional arrays in PHP
Generally, each element in an array is represented by characters or numbers, so the array elements can be arranged in ascending order. The functional function is sort(). For example:

Copy code The code is as follows:

$people=array('name', 'sex','nation','birth');
foreach ($people as $mychrs)
echo $mychrs." ";
sort($people);
echo "< br />---After sorting---
";
foreach ($people as $mychrs)
echo $mychrs." ";
?>

The array elements sorted in ascending order are displayed as birth name nation sex. Of course, the sort() function is case-sensitive (the order of letters from large to small is: A...Z...a...z)
The Sort() function also has a second parameter, which is used to indicate whether the ascending order rule is used to compare numbers or strings. For example:
Copy code The code is as follows:

echo "---Sort in ascending numerical order ---
";
$num2=array('26','3',);
sort($num2,SORT_NUMERIC);
foreach ($num2 as $mychrs )
echo $mychrs." ";
echo "
---Sort in ascending character order---
";
$num3=array('26 ','3');
sort($num3,SORT_STRING);
foreach ($num3 as $mychrs)
echo $mychrs." ";
?>

SORT_NUMERIC and SORT_STRING are used to declare ascending order of numbers or characters. If arranged in ascending order of numbers, it is: 3, 26; but if arranged in ascending order of characters, it is: 26, 3.
In addition to the ascending order function, there is also a descending or reverse sorting function in PHP, which is the rsort() function, such as:
Copy code Code As follows:

$num1=range(1,9);
rsort($num1);

This is actually equivalent to range(9,1)

3. Sorting of associative arrays in PHP
In addition to supporting numerical index arrays, PHP also supports associative arrays. For example, the following array is a related (associative) array:

Copy code The code is as follows:

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

Use sort ($peoples) defaults to sorting in ascending order of element definition values. In an associative array, it can be expressed using the asort() function. The most important thing in an associative array is that it can be sorted in ascending order of keywords (such as xm, xb, mz, etc.) Sorting, this method is to use the function ksort() function.
Copy code The code is as follows:

$peoples=array('xm'=> 'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
foreach ($peoples as $mychrs)
echo $mychrs." ";
echo "
--Sort in ascending order by element value--
";
asort($peoples);
foreach ($peoples as $mychrs)
echo $mychrs." ";
echo "
--sort by keyword in ascending order--
";
ksort($peoples) ;
foreach ($peoples as $mychrs)
echo $mychrs." ";
?>

Corresponding to the reverse sorting rsort() descending function that has the sort() ascending function for regular arrays, associative arrays also have corresponding descending functions: asort() function and arsort() function, ksort() function and krsort( )function.
Memory: The prototype function is sort(), where a and k represent associative arrays that must be preceded, and reverse sorting is modified with r.

4, PHP array elements are randomly sorted
The shuffle() function is used in PHP to randomly reorder the array elements, such as:

Copy code The code is as follows:

$fer=array('cnbruce','cnrose','cnjames',' cnanne');
shuffle($fer);
foreach ($fer as $mychrs)
echo $mychrs." ";
?>

Every Different sorting combinations will be displayed every time

5, PHP arrays are sorted in reverse order.
You can use the array_reverse() function in PHP to sort array elements in reverse order. For example:

Copy code The code is as follows:

$fer=array('cnbruce', 'cnrose','cnjames','cnanne');
foreach ($fer as $mychrs)
echo $mychrs." ";
$fer=array_reverse($fer);
echo "
--Reverse the original order--
";
foreach ($fer as $mychrs)
echo $mychrs." ";
?> ;

cnbruce cnrose cnjames cnanne
--Reverse the original order--
cnanne cnjames cnrose cnbruce
Note that $fer=array_reverse($fer); any sorting function before this just declares a reference and does not redefine the original array, but when I debug the function, it needs to be redefined. This is because array_reverse() returns a modified copy of the original array. If you do not need the original array, you can redefine the original array to overwrite it. Otherwise, define another array to save the copy, such as: $fer_bak=array_reverse ($fer);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327752.htmlTechArticle1. The function range() to quickly create an array. For example, the range() function can quickly create numbers from 1 to 9. Array: Copy the code as follows: ?php $numbers=range(1,9); echo $numbers[1]; ? Of course...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template