Home > Article > Backend Development > How to remove numeric index from array in php
Removal method: 1. Define a string array "$keys". The elements of this array are all strings. The number of elements is the same as the array "$arr" that needs to be processed; 2. Use "array_combine( $keys,$arr)" statement removes the numeric index in the "$arr" array and converts the numeric index into a string index.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the array_combine() function To remove the numeric index in the array and convert the numeric index into a string index.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("red","green","blue","yellow"); var_dump($arr); $keys=array("a","b","c","d"); echo "使用array_combine()去除数字索引后:"; var_dump(array_combine($keys,$arr)); ?>
Output result:
Let’s take a look at the array_combine() function.
array_combine() function creates a new array by merging two arrays, where the elements of one array are key names and the elements of the other array are key values.
Syntax:
array_combine($keys,$values)
The elements in the $keys array serve as the keys of the new array, and the elements of the $values array serve as the keys of the new array.
But it should be noted that when using the array_combine() function to create an array, the number of elements in the $keys array and the $values array must be consistent, so that the key names and key values can correspond one to one, otherwise An error will be reported and FALSE will be returned.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove numeric index from array in php. For more information, please follow other related articles on the PHP Chinese website!