Home  >  Article  >  Backend Development  >  How to change array key name to number in php

How to change array key name to number in php

青灯夜游
青灯夜游Original
2021-07-08 18:27:112750browse

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)
Parameters Description
array Required. 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));
?>

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)

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!

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