PHP 删除数组元素并重新索引
要删除数组元素并重新索引数组,代码如下-
示例
<?php$arr = array( " John ", "Jacob ", " Tom ", " Tim ");
echo "具有前导和尾随空格的数组...\n";
foreach( $arr as $value ) {
echo "Value = $value \n";
}
echo "\n逗号分隔列表...\n";
print_r(implode(', ', $arr));
$result = array_map('trim', $arr);
echo "\n更新后的数组...\n";
foreach( $result as $value ) {
echo "Value = $value \n";
}
unset($result[1]);
echo "\n更新后的数组...已重新索引\n";
$result2 = array_values($result);
foreach( $result2 as $value ) {
echo "Value = $value \n";
}
?>
输出结果
具有前导和尾随空格的数组...Value = John
Value = Jacob
Value = Tom
Value = Tim
逗号分隔列表...
John , Jacob , Tom , Tim
更新后的数组...
Value = John
Value = Jacob
Value = Tom
Value = Tim
更新后的数组...已重新索引
Value = John
Value = Tom
Value = Tim
以上是 PHP 删除数组元素并重新索引 的全部内容, 来源链接: utcz.com/z/327343.html