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

WBOY
Release: 2016-07-13 10:17:14
Original
1387 people have browsed it

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);
Copy after login

The result is:

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

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);
Copy after login

The result is:

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

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 =...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template