Differences between unset and array_splice in PHP to delete elements in an array. unsetarray_splice_PHP tutorial

Source: Internet
Author: User
In PHP, unset and array_splice are used to delete the elements in the array. The difference is unsetarray_splice. In PHP, unset and array_splice are used to delete elements in an array. if unsetarray_splice is used to delete an element in an array, unset can be used directly, but the array index will not rearrange the unset in PHP, what is the difference between array_splice and unsetarray_splice for deleting elements in an 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)

Delete special elements in an array

<?php$arr2 = array(1,3, 5,7,8);foreach ($arr2 as $key=>$value){  if ($value === 3)    unset($arr2[$key]);}var_dump($arr2);?> 

Add or delete an empty array

Instance:

<? Php $ array = ('a' => "abc", 'B' => "bcd", 'C' => "cde ", 'D' => "def", 'E' => ""); array_filter ($ array); echo"
";  print_r($array);?>


Result:

Array (
[A] => abc
[B] => bcd
[C] => cde
[D] => def
)

Summary

If the array_splice () function is deleted, the index value of the array also changes.
If the unset () function is deleted, the index value of the array remains unchanged.


What is the difference between unset and array_pop in php?

Unset can delete all variables. array_pop is only used for Array Operations. only the last element of the array is displayed. unset can delete any element in the array.

Php deletes elements in an array

Do you know the array_slice function?
$ Arr = array_slice ($ arr, 0, 3 );
You can.
Array_slice () The first parameter is the array to be cut, the second parameter is the starting position, and the third parameter is the length.
It is to cut the $ arr array and count it three times from 0th elements.
Array_slice is flexible in usage and supports negative number parameters. for details, refer to the php Manual.
Cn.php.net/manual/en/function.range.php

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...

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.