Home >Backend Development >PHP Tutorial >How to delete elements from PHP array (unset, array_splice)? _PHP Tutorial

How to delete elements from PHP array (unset, array_splice)? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:141410browse

How to delete elements in PHP array (unset, array_splice)?

If you want to delete an element in an array, you can use unset directly, but the index of the array will not be rearranged:

<?php  
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);

The result is:

Array ( [0] => a [2] => c [3] => d )

So how can the missing elements be filled and the array re-indexed? The answer isarray_splice():

<?php  
$arr = array('a','b','c','d');  
array_splice($arr,1,1);  
print_r($arr);

The result is:

Array ( [0] => a [1] => c [2] => d )

Articles you may be interested in

  • PHP removes null elements from an array (array_filter)
  • php finds whether a value exists in an array (in_array (),array_search(),array_key_exists())
  • How to delete the first and last elements of an array in php
  • Summary of JavaScript array operation functions (push, pop, join, shift, unshift, slice, splice, concat)
  • The difference between PHP merging arrays + and array_merge
  • php clears null elements in the array
  • PHP array function array_walk() notes
  • php gets the last element of the array

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/894202.htmlTechArticleHow to delete elements in PHP array (unset, array_splice)? If you want to delete an element in an array, you can use unset directly, but the index of the array will not be rearranged: ?php $arr =...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn