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

Source: Internet
Author: User

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

If you want to delete an element from 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 we fill in the missing elements and re-index the array? The answer is array_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 the null element from the array (array_filter)
  • Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
  • How does php Delete the first and last elements of an array?
  • JavaScript array operation function Summary (push, pop, join, shift, unshift, slice, splice, concat)
  • PHP merges arrays + and array_merge
  • Php clears the null element in the array.
  • PHP Array Function array_walk () Notes
  • Php retrieves the last element of the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.