Home > Article > Backend Development > How to convert one-dimensional array to associative array in PHP
PHP is a scripting language widely used in web development. Its powerful array functionality is one of its most important features.
In PHP, an array is a variable that can store multiple values, and these values can be any type of data, such as integers, floating point numbers, strings, Boolean values, objects, etc. In addition, arrays can also be divided into indexed arrays and associative arrays.
This article mainly introduces the method of converting one-dimensional array to associative array in PHP.
1. What is an associative array
In PHP, an associative array is also called a hash table or dictionary. It is a data structure that corresponds values to key names (or indexes), for example:
$person = array( 'name' => 'Tom', 'age' => 20, 'email' => 'tom@example.com' );
In this associative array, the strings named "name", "age", and "email" are The key name, and "Tom", "20", and "tom@example.com" are the corresponding values.
2. Convert one-dimensional array to associative array
The method of converting a one-dimensional array into an associative array is very simple. You only need to match the original array elements with key names and values.
Here is an example array:
$students = array('Max', 'John', 'Jane');
Convert it to an associative array and assign an ID to each element:
foreach($students as $key => $value) { $students[$key] = array( 'id' => $key, 'name' => $value ); }
In this code, $key
represents the array key name, and $value
represents the corresponding value. In the loop, we create a new array containing two key-value pairs, using $key
as the ID and $value
as the value of name.
The final result is like this:
array( 0 => array('id' => 0, 'name' => 'Max'), 1 => array('id' => 1, 'name' => 'John'), 2 => array('id' => 2, 'name' => 'Jane') )
As you can see, this one-dimensional array was successfully converted into an associative array, in which each element has an ID and corresponding name. .
3. Use the array_combine function to convert
In addition to using the foreach loop, PHP also provides a fast method to convert a one-dimensional array into an associative array. This method is to use the array_combine() function.
The array_combine() function can combine two arrays into a new array, with the first array as the key name and the second array as the corresponding key value.
The following code demonstrates how to use the array_combine() function to convert two arrays into an associative array:
$keys = array('name', 'age', 'city'); $values = array('John', 30, 'New York'); $person = array_combine($keys, $values);
In this code, we create two one-dimensional arrays $keys
and $values
respectively contain elements such as "name", "age", "city" and "John", "30", and "New York". Then use the array_combine() function to combine them into an associative array $person
.
The final result is:
array( 'name' => 'John', 'age' => 30, 'city' => 'New York' )
This associative array has three key-value pairs, each key name is "name", "age", "city", and the corresponding value is They are "John", "30", and "New York" respectively.
4. Summary
This article mainly introduces how to convert a one-dimensional array in PHP into an associative array. Either through a foreach loop or using the array_combine() function, both methods can easily accomplish this task. In actual development, which method to choose depends on different situations and needs.
The above is the detailed content of How to convert one-dimensional array to associative array in PHP. For more information, please follow other related articles on the PHP Chinese website!