Home Backend Development PHP Problem PHP reorders after deleting associative array

PHP reorders after deleting associative array

May 06, 2023 pm 01:16 PM

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 $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";
}
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 $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
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24