Home > Article > Backend Development > How to change the position of array elements in php
php can change the position of array elements by sorting the array elements. Sorting method: 1. Use sort() to sort the array elements in ascending order; 2. Use rsort() to sort the array elements in descending order; 3. Use shuffle() to randomly arrange the array elements, that is, randomly disrupt the order of the elements; 4. , asort(), etc.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can sort the array to change the position of the element. Commonly used array sorting methods are:
sort(): Sort array elements in ascending order
asort(): According to the key of the associated array Value, sort the array in ascending order
ksort(): Sort the array in ascending order according to the key name of the associated array
1. Use the sort() function
sort() function to sort the array elements in ascending order (from small to large, from low to high ).<?php header("Content-type:text/html;charset=utf-8"); $arr1 = array(10, 23, 5, 12, 84, 16); sort($arr1); var_dump($arr1); $arr2= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); sort($arr2); var_dump($arr2); ?>
2. Use the asort() function
asort() function will sort the associative array in ascending order according to the key value. And the key names in the original array will not be modified.<?php header("Content-type:text/html;charset=utf-8"); $age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16); asort($age); var_dump($age); ?>
3. Use the ksort() function
The ksort() function will sort the associative array in ascending order according to the key name. The key names in the original array will not be modified.<?php header("Content-type:text/html;charset=utf-8"); $arr= array("l"=>"lemon", "o"=>"orange", "b"=>"banana", "a"=>"apple"); ksort($arr); var_dump($arr); ?>
4. Use the rsort() function
rsort() function is used to sort the array elements in descending order, that is Sort from large to small, high to low. Let’s take a look at the code example:<?php header("Content-type:text/html;charset=utf-8"); $arr = array(2, 1, 5, 16, 29, 15); rsort($arr); var_dump($arr); ?>
5. Use the arsort() function
arsort( ) function will sort the associative array in descending order according to the key values, and will not modify the key names in the original array.<?php header("Content-type:text/html;charset=utf-8"); $age = array("张三"=>30,"李四"=>23,"王五"=>15,"李华"=>12,"娜娜"=>26,"小红"=>16); arsort($age); var_dump($age); arsort($age,2); var_dump($age); ?>
6. Use krsort() function
krsort() function will sort the associative array in descending order according to the key name. The key names in the original array will not be modified.<?php header("Content-type:text/html;charset=utf-8"); $arr= array("l"=>"lemon", "o"=>"orange", "b"=>"banana", "a"=>"apple"); krsort($arr); var_dump($arr); krsort($arr,1); var_dump($arr); ?>
7. Random sorting
In PHP, you can use the shuffle() function to randomly sort and disrupt the array elements. The shuffle() function will rearrange the elements in the array in random order.<?php header("Content-type:text/html;charset=utf-8"); $arr1 = array(10, 23, 5, 12, 84, 16); shuffle($arr1); var_dump($arr1); $arr2= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); shuffle($arr2); var_dump($arr2); ?>Output result:
PHP Video Tutorial"
The above is the detailed content of How to change the position of array elements in php. For more information, please follow other related articles on the PHP Chinese website!