Home > Backend Development > PHP Tutorial > PHP implementation method of deleting specified elements in an array based on key

PHP implementation method of deleting specified elements in an array based on key

黄舟
Release: 2023-03-06 10:58:01
Original
7518 people have browsed it

The following editor will bring you an implementation method of deleting specified elements in an array based on key. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let's follow the editor and take a look.

The elements in the php array exist in the form of key-value pairs ('key'=>'value'). Sometimes we need to delete the specified items in the array based on the key. an element of .

  function bykey_reitem($arr, $key){ 
    if(!array_key_exists($key, $arr)){ 
      return $arr; 
    } 
    $keys = array_keys($arr); 
    $index = array_search($key, $keys); 
    if($index !== FALSE){ 
      array_splice($arr, $index, 1); 
    } 
    return $arr; 

  } 
  $data = array('name'=>'apple','age'=>12,'address'=>'ChinaGuangZhou'); 
  $result = array_remove($data, 'name'); 
  var_dump($result);
Copy after login

Instructions for using functions:

1. array_search()

Definition and Usage

array_search() function is the same as in_array(), searching for a key value in an array. If the value is found, the key of the matching element is returned. If not found, returns false.

Prior to PHP 4.2.0, functions returned null instead of false on failure.

If the third parameter strict is specified as true, the key name of the corresponding element will only be returned if the data type and value are consistent.

Syntax

array_search(value,array,strict)
Parameter Description
value Required. Specifies the value to search for in the array.
array Required. The array to be searched.
strict Optional. Possible values:
true
false - Default

If the value is set to true, the type of the given value will also be checked in the array

Example 1

  <?php
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    echo array_search("Dog",$a);
  ?>
Copy after login

2. array_splice()

Definition and usage

array_splice() function Similar to the array_slice() function, selects a range of elements in an array, but instead of returning them, deletes them and replaces them with other values.

If the fourth parameter is provided, those previously selected elements will be replaced by the array specified by the fourth parameter. The final generated array will be returned.

Syntax

##array_splice(array,offset,length,array)

Parameter Description

array Required. Specifies an array.

offset Required. numerical value. If offset is positive, removal begins at the offset specified by this value in the input array. If offset is negative, removal begins at the offset specified by this value from the end of the input array.

length Optional. numerical value. If this parameter is omitted, all parts of the array from offset to the end are removed. If length is specified and is positive, this many elements are removed. If length is specified and is negative, all elements from offset to length counting down from the end of the array are removed.

array The removed elements are replaced by elements in this array. If no values ​​are removed, the element in this array is inserted at the specified position.

Tips and Notes

Tip: If the function does not remove any elements (length=0), the substitution array will be removed from the position of the start parameter insert. (See Example 3)

Note: Keys in the substitution array are not retained.

Example

<?php
     $a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
     $a2=array(0=>"Tiger",1=>"Lion");
     array_splice($a1,0,2,$a2);
     print_r($a1);
     //输出:

    Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
?>
    //与例子 1 相同,但是输出返回的数组:

<?php
     $a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
     $a2=array(0=>"Tiger",1=>"Lion");
     print_r(array_splice($a1,0,2,$a2));
?>

    //输出:

    Array ( [0] => Dog [1] => Cat )
    //length 参数设置为 0:

<?php
     $a1=array(0=>"Dog",1=>"Cat");
     $a2=array(0=>"Tiger",1=>"Lion");
     array_splice($a1,1,0,$a2);
     print_r($a1);
?>

   //输出:
   Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )
Copy after login
The above is the content of the PHP implementation method of deleting the specified elements in the array according to the key. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!

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