Detailed explanation of steps to delete value elements in one-dimensional array in PHP

php中世界最好的语言
Release: 2023-03-26 20:32:02
Original
1673 people have browsed it

This time I will bring you PHP deletionOne-dimensional arrayDetailed explanation of the steps of median element, PHPNotesWhat are the precautions for deleting the median element of one-dimensional array? The following is a practical case , let’s take a look.

1. Write your own for loop

Remove the value of the $tmp element from the array

<?php
$tmp = &#39;324&#39;;
$arr = array(
&#39;0&#39; => '321',
'1' => '322',
'2' => '323',
'3' => '324',
'4' => '325',
'5' => '326',
);
Copy after login

Code

foreach( $arr as $k=>$v) {
 if($tmp == $v) unset($arr[$k]);
}
print_r($arr);
?>
Copy after login

At this time

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [4] => 325
 [5] => 326
)
Copy after login

To reset the index, add a sentence

foreach( $arr as $k=>$v) {
 if($tmp == $v) unset($arr[$k]);
}
$arr = array_values($arr);
print_r($arr);
?>
Copy after login

At this time the result

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)
Copy after login
Copy after login
Copy after login
Copy after login

array_merge() can also achieve the same effect

foreach( $arr as $k=>$v) {
 if($tmp == $v) unset($arr[$k]);
}
$arr = array_merge($arr);
print_r($arr);
?>
Copy after login

At this time the result

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)
Copy after login
Copy after login
Copy after login
Copy after login

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.

$key=array_search($tmp ,$arr);
array_splice($arr,$key,1);
var_dump($arr);
Copy after login

Result at this time

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)
Copy after login
Copy after login
Copy after login
Copy after login

Best Practice

$arr = array_merge(array_diff($arr, array($tmp)));
var_dump($arr);
Copy after login

Result

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)
Copy after login
Copy after login
Copy after login
Copy after login

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

$arr = array_merge(array_diff($arr, $tmp));
var_dump($arr);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to implement regular expression group capture in PHP

Detailed explanation of the use of automatic loading of PHP files

The above is the detailed content of Detailed explanation of steps to delete value elements in one-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template