4 回答
TA贡献1866条经验 获得超5个赞
删除一个数组元素
\unset()
\array_splice()
.
\array_search()
\unset()
方法
\unset()
\array_values()
\unset()
<?php $array = [0 => "a", 1 => "b", 2 => "c"]; \unset($array[1]); //↑ Key which you want to delete?>
[ [0] => a [2] => c]
\array_splice()
方法
\array_splice()
\array_values()
\array_splice()
<?php $array = [0 => "a", 1 => "b", 2 => "c"]; \array_splice($array, 1, 1); //↑ Offset which you want to delete?>
[ [0] => a [1] => c]
array_splice()
\unset()
删除多个数组元素
\unset()
\array_splice()
\array_diff()
\array_diff_key()
\array_diff()
方法
\array_diff()
\unset()
<?php $array = [0 => "a", 1 => "b", 2 => "c"]; $array = \array_diff($array, ["a", "c"]); //└────────┘→ Array values which you want to delete?>
[ [1] => b]
\array_diff_key()
方法
\array_diff_key()
\array_flip()
<?php $array = [0 => "a", 1 => "b", 2 => "c"]; $array = \array_diff_key($array, [0 => "xy", "2" => "xy"]); //↑ ↑ Array keys which you want to delete?>
[ [1] => b]
\unset()
\array_splice()
\array_keys()
TA贡献1796条经验 获得超10个赞
unset()
$array = array(0, 1, 2, 3);unset($array[2]);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [3]=> int(3) } */$array = array(0, 1, 2, 3);array_splice($array, 2, 1);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3) } */
array_splice()
array_values()
unset()
:
$array = array(0, 1, 2, 3);unset($array[2]);$array = array_values($array);var_dump($array);/* array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(3) } */
TA贡献1836条经验 获得超4个赞
// Our initial array $arr = array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red"); print_r($arr); // Remove the elements who's values are yellow or red $arr = array_diff($arr, array("yellow", "red")); print_r($arr);
Array( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red)Array( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo)
$arr = array_merge(array_diff($arr, array("yellow", "red")));print_r($arr);
Array( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo)
TA贡献1841条经验 获得超3个赞
$key = array_search($needle, $array);if ($key !== false) { unset($array[$key]);}
- 4 回答
- 0 关注
- 1073 浏览
添加回答
举报