$example = array('First','Second','Third','Fourth','Fifth',...
下面的代码将取消设置$example
中的第三个元素。
unset($example[2]);
如何unset
所有元素都超过第三个元素?
我希望$example
的值之后是这样的:
$example = array('First','Second','Third');
不要使用 unset,请使用array_splice:
$example = array('First','Second','Third','Fourth','Fifth');
array_splice($example,3);
//$example = array('First','Second','Third');
您可以使用array_splice
。
$example = array('First','Second','Third','Fourth','Fifth');
array_splice($example, 3);
var_dump($example);
array
0 => string 'First' (length=5)
1 => string 'Second' (length=6)
2 => string 'Third' (length=5)
试试这个:
for ($i = count($array); $i > 3; $i--)
{
array_pop($array);
}