Home >Backend Development >PHP Problem >How to remove array key value in php
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.
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); ?>
##Method 2: Use array_splice() function
array_slice() function is used to intercept an array, that is, to extract a fragment from the arrayUsage syntax:array_splice($array,$start,$length)Parameters:
<?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); ?>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); ?>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!