How to convert string to array in php and sort it

PHPz
Release: 2023-04-18 15:45:05
Original
452 people have browsed it

In PHP, converting a string to an array is a common task. However, after converting the string to an array, you may need to sort the array. In this article, we will explain how to convert a string into an array and sort it.

Convert a string to an array

In PHP, there are multiple ways to convert a string to an array. The following is one of the methods:

$string = "apple,banana,orange";
$array = explode(",", $string);
Copy after login

In this example, we use the explode() function. The first parameter is the delimiter used to split the string, and the second parameter is the string to be split. This function will return an array whose value is the result of the string being split.

You can use the print_r() function to view the contents of the array:

print_r($array);
Copy after login
Copy after login

This will output the following:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login
Copy after login

Sort the array

In PHP , there are multiple ways to sort an array. The following is one of the methods:

sort($array);
Copy after login

In this example, we use the sort() function to sort the array. This function will sort the array alphabetically and modify the original array.

You can use the print_r() function to view the sorted array contents:

print_r($array);
Copy after login
Copy after login

This will output the following:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login
Copy after login

If you want to sort by word length, you can use The following code:

usort($array, function($a, $b) {
    return strlen($a) - strlen($b);
});
Copy after login

In this example, we use the usort() function to sort the array. The first parameter is the array to be sorted, and the second parameter is the callback function used to compare elements. In this callback function, we use the strlen() function to get the length of each word and return the comparison result between its lengths.

You can use the print_r() function to view the contents of the array sorted by length:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)
Copy after login

Summary

In this article, we introduced how to convert a string into an array and sort them. Use the explode() function to convert a string into an array, the sort() function to sort an array alphabetically, and the usort() function to sort an array by an element-specific comparison function. These functions help you manage and manipulate arrays in PHP.

The above is the detailed content of How to convert string to array in php and sort it. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!