Home > Backend Development > PHP Problem > How to remove numeric index from array in php

How to remove numeric index from array in php

青灯夜游
Release: 2023-03-12 18:12:02
Original
2189 people have browsed it

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.

How to remove numeric index from array in php

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));
?>
Copy after login

Output result:

How to remove numeric index from array in php

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template