How to change array key name to number in php

青灯夜游
Release: 2023-03-11 11:20:01
Original
2819 people have browsed it

In PHP, you can use the array_values() function to change the array key name to a number. This function can get the values ​​of all elements in the array and convert the associative array into an index array. Then the key name of the array It becomes a number; the syntax is "array_values(array)".

How to change array key name to number in php

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

In php, you can use array_values () function to change the array key name to a number.

array_values() function returns an array containing all the values ​​in the array.

Grammar format:

array_values(array)
Copy after login
ParametersDescription
arrayRequired. Specifies an array.

array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.

Return value: Returns an array containing all the values ​​in the array. The returned array will use numeric keys, starting at 0 and increasing by 1.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$array = array("颜色1" => &#39;红色&#39;, "颜色2" => &#39;黄色&#39;, "颜色3" => &#39;蓝色&#39;, "颜色4" => &#39;紫色&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump(array_values($array));
?>
Copy after login

Output:

array (size=4)
  0 => string &#39;红色&#39; (length=6)
  1 => string &#39;黄色&#39; (length=6)
  2 => string &#39;蓝色&#39; (length=6)
  3 => string &#39;紫色&#39; (length=6)
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to change array key name to number 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