Home > Backend Development > PHP Problem > How to convert associative array to ordinary array in php

How to convert associative array to ordinary array in php

青灯夜游
Release: 2023-03-16 14:24:02
Original
2179 people have browsed it

In PHP, you can use the array_values() function to convert an associative array into a normal array (index array), and the syntax is "array_values($array)". The array_values() function returns an array containing all the values ​​in the given array, but does not retain the key names; the returned array will be in the form of an index array, and the index of the array starts from 0 and increases by 1.

How to convert associative array to ordinary array in php

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

In the PHP array, no matter what type of key Each name will have a value corresponding to it, that is, a key/value pair. According to the different data types of the array key names, PHP arrays can be divided into two types:

  • Using numbers as keys The name is called an Indexed Array - an ordinary array

  • An array with a string or a mixture of strings and numbers as keys is called an Associative Array.

php method to convert an associative array into a normal array (index array)

In PHP, you can use the array_values() function to convert an associative array into an ordinary array (index array) The array is converted into a normal array (indexed array).

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 $array, it can return a value containing all 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.

array_values($array)
Copy after login

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

Code sample:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(1=>"1","a"=>"",2=>"2","b"=>0,"c"=>"blue");
echo "原关联数组:";
var_dump($arr);
$res=array_values($arr);
echo "转换后的数组:";
var_dump($res);
?>
Copy after login

How to convert associative array to ordinary array in php

Note: The array_values() function only applies to one-dimensional arrays, for multi-dimensional arrays other than the first dimension Dimensions have no effect.

But you can get the value by accessing the array in the multidimensional array:

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(1,2,3,"aa"=>array(4,5,6),7,8,array(9,10)); 
echo "原关联数组:";
var_dump($arr);
$res=array_values($arr["aa"]);
echo "转换后的数组:";
var_dump($res);
?>
Copy after login

How to convert associative array to ordinary array in php

Recommended learning: "PHP Video Tutorial"

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