Home  >  Article  >  Backend Development  >  How to convert associative array to index array in php

How to convert associative array to index array in php

青灯夜游
青灯夜游Original
2021-09-09 18:40:582738browse

In PHP, you can use the array_values() function to convert an associative array into an index array. You can use this function to reset the array key name and convert the string or numerically confusing key name to one starting from 0 and Numeric key name incremented by 1; use syntax "array_values($array)".

How to convert associative array to index array in php

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

In PHP, you can use the array_values() function to convert an associative array into an indexed array.

array_values($array) The 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 a value containing all the elements in the given array. Array of values, but no 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, you can use this function to reset the array key name and convert the key name with confusing string or numerical value into a numeric key name starting from 0 and increasing by 1.

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

Example: Use the array_values() function to convert an associative array into an indexed array

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

Output result:

How to convert associative array to index array in php

If you want to use array_values() to reset the index of a multi-dimensional array, you can refer to the article "PHP array learning how to reset the index of a multi-dimensional array"

Recommended study: "PHP video tutorial

The above is the detailed content of How to convert associative array to index array 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