How to sort and search arrays in PHP

王林
Release: 2023-09-05 13:50:02
Original
746 people have browsed it

PHP 数组如何排序和搜索

How to sort and search PHP arrays

Overview:
In PHP, an array is a very commonly used data structure used to store and operate a set of related data elements. Sorting and searching arrays are common problems in programming. This article will introduce how to sort and search arrays in PHP, and give corresponding code examples.

1. Array sorting

PHP provides a variety of functions to sort arrays. The following are several commonly used array sorting methods:

  1. sort() function: Sort the array in ascending order. The sample code is as follows:
$fruits = array("orange", "apple", "banana"); sort($fruits); print_r($fruits);
Copy after login

Output result: Array ([0] => apple [1] => banana [2] => orange )

  1. rsort() function: Sorts the array in descending order. The sample code is as follows:
$fruits = array("orange", "apple", "banana"); rsort($fruits); print_r($fruits);
Copy after login

Output result: Array ([0] => orange [1] => banana [2] => apple )

  1. asort() function: Sort the array in ascending order and retain the key names. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); asort($fruits); print_r($fruits);
Copy after login

Output result: Array ( [a] => apple [c] => banana [b] => orange )

  1. arsort() function: Sort the array in descending order and retain the key names. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); arsort($fruits); print_r($fruits);
Copy after login

Output result: Array ([b] => orange [c] => banana [a] => apple )

  1. ksort() function: Sort the array in ascending order by key name. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); ksort($fruits); print_r($fruits);
Copy after login

Output result: Array ( [a] => apple [b] => orange [c] => banana )

  1. krsort() function: Sort the array in descending order by key name. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); krsort($fruits); print_r($fruits);
Copy after login

Output result: Array ([c] => banana [b] => orange [a] => apple )

2. Array Search

PHP provides a variety of functions to search arrays. The following are several commonly used array search methods:

  1. in_array() function: Search the specified value in the array and return a Boolean value. The sample code is as follows:
$fruits = array("orange", "apple", "banana"); if (in_array("apple", $fruits)) { echo 'Found'; } else { echo 'Not Found'; }
Copy after login

Output result: Found

  1. array_search() function: Search the specified value in the array and return the corresponding key name. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); $key = array_search("apple", $fruits); echo "Key: " . $key;
Copy after login

Output result: Key: a

  1. array_key_exists() function: Check whether the specified key name or index exists in the array and return a Boolean value . The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); if (array_key_exists("b", $fruits)) { echo 'Exists'; } else { echo 'Not Exists'; }
Copy after login

Output result: Exists

  1. array_values() function: Returns all the values in the array and re-indexes. The sample code is as follows:
$fruits = array("b" => "orange", "a" => "apple", "c" => "banana"); $values = array_values($fruits); print_r($values);
Copy after login

Output result: Array ( [0] => orange [1] => apple [2] => banana )

Summary:
This article introduces the methods of sorting and searching arrays in PHP, and gives corresponding code examples. I hope it will be helpful for beginners to have a deeper understanding and application of PHP array related operations.

The above is the detailed content of How to sort and search arrays 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!