How to change the position of array elements in php

青灯夜游
Release: 2023-03-16 08:52:01
Original
2315 people have browsed it

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.

How to change the position of array elements in php

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

  • ##rsort() : Sort the array elements in descending order

  • arsort(): Sort the array in descending order according to the key value of the associated array

  • krsort() : Sort the array in descending order according to the key name of the associative array

  • shuffle(): Rearrange the elements in the array in random order.

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

How to change the position of array elements in php

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);
?>
Copy after login

Output result:


How to change the position of array elements in php

Refresh the result and the result will change:

How to change the position of array elements in php

Description: The shuffle() function will not only randomly shuffle the array, but also delete the original key names in the array and assign them new numerical key names (that is, the index relationship will not be maintained).

Recommended learning: "

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!

Related labels:
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