The following editor will share with you an article on how to delete a certain value element in a one-dimensional array in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.
1. Write your own for loop
Remove the value of the $tmp element from the array
1 2 3 4 5 6 7 8 9 10 |
|
Code
1 2 3 4 5 |
|
at this time
1 2 3 4 5 6 7 8 |
|
To reset the index, add
1 2 3 4 5 6 |
|
The result at this time
1 2 3 4 5 6 7 8 |
|
array_merge() can also achieve the same effect
1 2 3 4 5 6 |
|
The result at this time
1 2 3 4 5 6 7 8 |
|
2 .Prefer using the functions that come with PHP, because they are implemented in C and are more efficient than writing them yourself.
Use array_search and array_splice, where array_splice automatically resets the sequence value.
1 2 3 |
|
The result at this time
1 2 3 4 5 6 7 8 |
|
Best Practices
1 2 |
|
Results
1 2 3 4 5 6 7 8 |
|
Here, if the array elements are complex data structures, comparison can also be achieved. Of course the data itself is still one-dimensional.
In the above example, $tmp is a value. If $tmp is an array or other complex data structure, delete all elements contained in $tmp from $array. The above method is also valid.
1 2 |
|
The above PHP method of deleting a certain value element in a one-dimensional array is all the content shared by the editor. I hope it can give you a reference, and I hope Please support php Chinese website.
PHP implements the inverse color processing function of pictures
Example of php installing extension through pecl method to explain php skills
php study notes: basic usage of mb_strstr php skills
The above is the detailed content of How to delete a certain value element in a one-dimensional array using php techniques. For more information, please follow other related articles on the PHP Chinese website!