Home  >  Article  >  Backend Development  >  How to remove array key value in php

How to remove array key value in php

青灯夜游
青灯夜游Original
2022-04-20 17:12:073517browse

php method to remove array key values: 1. Use the "array_keys($array)" statement to remove all key values ​​and return an array containing all key names; 2. Use "array_splice($array,$ start,$length)" statement can remove one or more key values ​​at the specified position.

How to remove array key value in php

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

There are many ways to remove array key values ​​in php , two methods are introduced below:

Method 1: Use array_keys() function

array_key() function can get some or all key names in the array.

Use the array_key() function to remove all key values ​​​​from the array and return a new array containing all key names.

Usage syntax:

array_keys($array)

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
var_dump($arr);
$keys=array_keys($arr);
var_dump($keys);
?>

How to remove array key value in php

##Method 2: Use array_splice() function

array_slice() function is used to intercept an array, that is, to extract a fragment from the array

Usage syntax:


array_splice($array,$start,$length)

Parameters:

  • array represents the array to be intercepted.

  • start indicates the starting position (subscript) of interception:
    • If start is a positive number, interception is performed from front to back.
    • If start is a negative number, start from the position -start from the end of arr and intercept from back to front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the intercepted length:
    • If length is a positive number, it indicates the number of intercepted elements;
    • If length If it is a negative number, then the intercepted fragment will end at the length position from the end of the array;
    • If it is omitted, it will start from the start position and continue to the end of the array.
Use the array_splice() function to remove one or more key values ​​at the specified position.

Example 1:


<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

echo "删除后的数组:" ;
array_splice($arr,2);
var_dump($arr);
?>

How to remove array key value in php

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

echo "删除后的数组:" ;
array_splice($arr,2,-1);
var_dump($arr);
?>

How to remove array key value in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove array key value 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