1. array_shift()函数
删除数组中第一个元素,并返回该元素的值。如果数组为空则返回null。
示例:
<code><span><?php </span><span>$test_arr</span> = <span>array</span> ( <span>"name"</span> => <span>"jeanphorn"</span>, <span>"skill"</span> => <span>"programming"</span>, <span>"hobby"</span> => <span>"swimming"</span>, ); <span>echo</span> array_shift(<span>$test_arr</span>). <span>"\n"</span>; print_r(<span>$test_arr</span>); <span>?></span></span></code>
结果:
jeanphorn
Array (
[skill] => programming
[hobby] => swimming )
注意: 如果键名是数字的,所有元素都会获得新的键名,从 0 开始,并以 1 递增
2. array_pop()函数
删除数组中的最后一个元素。如果数组为空则返回null。
示例:
<code><span><?php </span><span>$test_arr</span> = <span>array</span> ( <span>"name"</span> => <span>"jeanphorn"</span>, <span>"skill"</span> => <span>"programming"</span>, <span>"hobby"</span> => <span>"swimming"</span>, ); <span>echo</span> array_pop(<span>$test_arr</span>). <span>"\n"</span>; print_r(<span>$test_arr</span>); <span>?></span></span></code>
以上就介绍了PHP 删除数组第一个元素和最后一个元素,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。