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 ];
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']);
In this way, the element with the key'Jerry'
in the$students
array 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"; }
After executing this code, we may get the result:
Tom's score is 75 Alice's score is 95
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; });
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$a
and$b
.
After using the above code to sort the$students
array, we can get the following results:
Alice's score is 95 Tom's score is 75
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"; }
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!