Home  >  Article  >  Backend Development  >  How to delete a value element in a one-dimensional array in php

How to delete a value element in a one-dimensional array in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-06 13:27:443045browse

php 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.

How to delete a value element in a one-dimensional array in php

php method to delete a value element in a one-dimensional array:

1. Write it yourself for loop

Remove $tmpthe value of this element from array

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

code

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

at this time

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

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);
?>

The result at this time

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

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);
?>
Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)

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);

Result at this time

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

Best practice

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

Result

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

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);

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!

Statement:
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