Home  >  Article  >  Backend Development  >  How to delete array elements based on key in php

How to delete array elements based on key in php

青灯夜游
青灯夜游Original
2021-02-26 15:44:282279browse

php method to delete array elements based on key: First use the array_keys() function and array_search() function to find the starting position x of the element to be deleted based on the key; then use "array_splice(array, x, 1);" to delete the specified array element.

How to delete array elements based on key in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The elements in the php array exist in the form of keys Value pair method ('key'=>'value'), sometimes we need to delete a specified element in the array based on the key.

<?php
function array_remove($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(&#39;name&#39;=>&#39;apple&#39;,&#39;age&#39;=>12,&#39;address&#39;=>&#39;ChinaGuangZhou&#39;);
$result = array_remove($data, &#39;age&#39;);
var_dump($result);
 ?>

Output:

array (size=2)
  &#39;name&#39; => string &#39;apple&#39; (length=5)
  &#39;address&#39; => string &#39;ChinaGuangZhou&#39; (length=14)

Instructions for using the function:

1.array_search()

Definition and usage

array_search() function is the same as in_array(), searching for a key value in the 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

[Recommended learning:《 PHP video tutorial》】

Example:

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

2.array_splice()

Definition and usage

array_splice The () function is similar to the array_slice() function, selecting a series of elements in the array, but instead of returning them, it 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 last 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 delete any elements (length=0), the substitution array will be inserted from the position of the start parameter.

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 )

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to delete array elements based on key in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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