PHP reorders after deleting associative array

WBOY
Release: 2023-05-06 13:16:07
Original
501 people have browsed it

When performing PHP array operations, we often need to add, delete, and modify the array. Deleting array elements may cause the internal order of the array to change, and if you use a foreach loop to traverse the array, it may cause unexpected results. This article will introduce a method to avoid traversal problems caused by deleting elements by deleting associative array elements and then reordering them.

First, let’s take a look at the array types in PHP.

There are two types of arrays in PHP: indexed arrays and associative arrays. Indexed arrays are integer-indexed arrays starting from 0, while associative arrays use strings as keys. The following is an example of an associative array:

$students = [ 'Tom' => 75, 'Jerry' => 82, 'Alice' => 95 ];
Copy after login

In this array, each element has a string key name and a corresponding value.

Now, we consider how to delete an element from the associative array$students. This can be achieved using the unset() function:

unset($students['Jerry']);
Copy after login

In this way, the element with the key'Jerry'in the$studentsarray can be deleted.

However, after deleting elements in this way, the internal order in the array changes, which may cause problems when we traverse the array. For example:

foreach ($students as $name => $score) { echo "$name's score is $score\n"; }
Copy after login

After executing this code, we may get the result:

Tom's score is 75 Alice's score is 95
Copy after login

And'Jerry''s score is not output because it has has been deleted. The root cause of this problem is that when we use the foreach loop to traverse the array, we proceed in the internal order of the array, and after the unset() function deletes the elements, the internal order of the array changes.

The solution to this problem is very simple: just re-sort the array after deleting the elements to avoid traversal problems caused by the internal order of the array. In PHP, we can use uasort() function to sort associative arrays by value. The usage of this function is as follows:

uasort($array, function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; });
Copy after login

uasort() The first parameter of the function is the array to be sorted, and the second parameter is the callback function used to compare two values. In the above code, we use an anonymous function to define the callback function, and return the corresponding comparison result by comparing the sizes of$aand$b.

After using the above code to sort the$studentsarray, we can get the following results:

Alice's score is 95 Tom's score is 75
Copy after login

Now, we have successfully avoided the problem caused by deleting associative array elements. traversal problem.

The complete sample code is as follows:

$students = [ 'Tom' => 75, 'Jerry' => 82, 'Alice' => 95 ]; unset($students['Jerry']); uasort($students, function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); foreach ($students as $name => $score) { echo "$name's score is $score\n"; }
Copy after login

In this article, we introduce how to reorder associative array elements after deleting them in PHP to avoid traversal problems caused by deleting elements. By sorting associative arrays, we can ensure that we get the correct results when traversing the array, improving the stability and reliability of programming.

The above is the detailed content of PHP reorders after deleting associative array. 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
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!