Home > Backend Development > PHP Problem > How to distinguish the difference between unset and array_splice in PHP

How to distinguish the difference between unset and array_splice in PHP

autoload
Release: 2023-03-09 09:42:02
Original
1907 people have browsed it

How to distinguish the difference between unset and array_splice in PHP

1. Function used

a. Function unset()

unset ( mixed $var , mixed $... = ? ) : void
Copy after login

          unset() Destroy the specified variable.

b. Functionarray_slice()

  array_splice(array,start,length,array)
Copy after login
  • Array represents an array.

  • start indicates the starting position of deleted elements.

  • length represents the number of elements removed and is also the length of the returned array. (Optional)

  • array represents an array with elements to be inserted into the original array (Optional)

2. Example:

Use unset() to delete an element in the array

<?php  
    $arr = array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;); 
    unset($arr[1]); 
    print_r($arr); 
?>
Copy after login

Output:

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

Use array_splice()Delete an element in the array

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

​​ Output:

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

Recommendation: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of How to distinguish the difference between unset and array_splice in PHP. For more information, please follow other related articles on the PHP Chinese website!

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