


How to delete a value element in a one-dimensional array in php
Aug 06, 2020 pm 01:27 PMphp method to delete a value element in a one-dimensional array: 1. Use the for loop statement and the function [array_merge()] to remove the value, the code is [$arr = array_merge($arr)]; 2. Use the function [array_splice] to automatically reset the sequence value.
php method to delete a value element in a one-dimensional array:
1. Write it yourself for loop
Remove $tmp
the value of this element from 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 a sentence
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 |
|
1 2 3 4 5 6 7 8 |
|
2. Prioritize using PHP’s built-in functions, 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 |
|
Result at this time
1 2 3 4 5 6 7 8 |
|
Best practice
1 2 |
|
Result
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 |
|
Related learning recommendations: php programming (video)
The above is the detailed content of How to delete a value element in a one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
