PHP Deletes the specified element in the array according to key

  • 2021-11-29 23:13:11
  • OfStack

Elements in an php array exist as key-value pairs ('key' = > 'value'), sometimes we need to delete an element specified in the array based on the key.


  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); 

Description of using functions:

1.array_search()

Definition and usage

array_search() Function and in_array() 1, look up 1 key value in the array. If the value is found, the key name of the matching element will be returned. If it is not found, false is returned.

Before PHP 4.2. 0, the function returned null instead of false on failure.

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

Grammar


array_search(value,array,strict)

Parameter description
value required. Specifies the values searched in the array.
array required. The array being searched.
strict is optional. Possible values:
true
false Default
If the value is set to true, the type of the given value is also checked in the array

Example 1


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

2.array_splice()

Definition and usage

array_splice() Function and array_slice() The function selects a series of 1 elements in an array, but does not return them, but deletes them and replaces them with other values.

If the fourth parameter is supplied, those previously selected elements are replaced by the array specified by the fourth parameter.

The last generated array will be returned.

Grammar


array_splice(array,offset,length,array)

Parameter description
array required. Specify the array.
offset Required. Numerical value. If offset is positive, it is removed from the offset specified by the value in the input array. If offset is negative, it is removed from the end of the input array by the offset specified by the reciprocal value.
length is 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 positive, so many elements are removed. If length is specified and negative, all elements from offset to the reciprocal length at the end of the array are removed.
array Elements removed are replaced by elements in this array. If no values are removed, the elements in this array are inserted into the specified position.

Tips and Notes

Tip: If the function does not delete any elements (length=0), the substitute array is inserted from the position of the start parameter.

Note: The keys in the substitute array are not preserved.

Instances


    <?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);
     // Output: 
    Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
    ?>
    // And examples  1  Same, but the array returned by the output: 
    <?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));
    ?>
    // Output: 
    Array ( [0] => Dog [1] => Cat )
    //length  Parameter is set to  0 : 
    <?php
     $a1=array(0=>"Dog",1=>"Cat");
     $a2=array(0=>"Tiger",1=>"Lion");
     array_splice($a1,1,0,$a2);
     print_r($a1);
   ?>
   // Output: 
   Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )

Summarize


Related articles: