Home  >  Article  >  Backend Development  >  Can php reset the key name of an array?

Can php reset the key name of an array?

青灯夜游
青灯夜游Original
2022-05-30 20:13:002049browse

php can reset the key name of the array. In PHP, you can use the array_values() function to reset the key name of the array. The function of this function is to return the values ​​of all elements in the array. It can reset the key name to a numerical key starting from 0 and increasing by 1. Syntax "array_values($array)".

Can php reset the key name of an array?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you can use array_values () function to reset the key name of the array.

array_values($array)
  • The parameter $array is the array being operated on.

array_values() function is to return the values ​​of all elements in the array. It is very simple to use. With only one required parameter, you can return an array containing all the values ​​in the given array. Array, but does not retain key names. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.

Simply put, array_values() will convert the specified array into an index array, and the key name of the array will be reset, starting from 0 and increasing by 1.

Example:

<?php
$arr1=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr1);
var_dump(array_values($arr1));

$arr2=array(2=>65,8=>80,5=>78,0=>90);
var_dump($arr2);
var_dump(array_values($arr2));
?>

Can php reset the key name of an array?

Description:

array_values() function is particularly suitable for confusing element subscripts in arrays Array, or used to convert an associative array into an indexed array.

But the array_values() function only applies to one-dimensional arrays and has no effect on dimensions other than the first dimension in multi-dimensional arrays.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can php reset the key name of an array?. 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