如何从PHP中的数组中删除元素并重新索引该数组?
可以使用'unset'函数从数组中删除一个元素,并使用'array_values'函数来重置数组的索引。
示例
<?php $my_arr = array( 'this', 'is', 'a', 'sample', 'only'); echo"The array is "; var_dump($my_arr); unset($my_arr[4]); echo"The array is now "; $my_arr_2 = array_values($my_arr); var_dump($my_arr_2); ?>
输出结果
The array is array(5) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
[4]=>
string(4) "only"
}
The array is now array(4) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "sample"
}声明一个包含字符串值的数组。显示该数组,并使用“取消设置”功能从该数组中删除特定的索引元素。然后再次显示该数组以反映控制台上的更改。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短