Home  >  Article  >  Backend Development  >  Conversion of php index and associative array

Conversion of php index and associative array

王林
王林Original
2023-05-19 10:38:37430browse

PHP is a very popular server-side scripting language used to create dynamic web pages. In PHP, array is an important data type used to store a set of data. Arrays in PHP can be divided into two types, indexed arrays and associative arrays. Indexed arrays use numeric keys as indexes, while associative arrays use string keys as indexes. This article will discuss how to convert indexed arrays and associative arrays in PHP.

  1. Convert index array to associative array

Converting index array to associative array is a common operation. You can use each value of the index array as a key of an associative array and assign a default value to each key. Here is an example that demonstrates how to convert an index array into an associative array:

$index_array = array('apple', 'banana', 'orange');
$length = count($index_array);
$default_value = 0;

$associative_array = array();
for ($i = 0; $i < $length; $i++) {
    $key = $index_array[$i];
    $associative_array[$key] = $default_value;
}

print_r($associative_array);

In the above code, we first create an index array $index_array, which contains the names of three fruits. We also define a default value of 0 that will be assigned to all keys of the newly created associative array. Next, we create an empty associative array $associative_array and use a loop to iterate over all values ​​of the index array. For each value, we set it as a key of the associative array and assign it a default value of 0. Finally, we use the print_r() function to output the newly created associative array. Running this code will output the following results:

Array
(
    [apple] => 0
    [banana] => 0
    [orange] => 0
)

In the above output, we can see that we have successfully created a new associative array where each key is a value in the index array , and assigned the default value 0.

  1. Convert associative array to index array

There are some issues that need to be paid attention to when converting associative array to index array. If the keys of an associative array are strings, these keys must be converted to numbers in order to be used as the keys of the new indexed array. This can be done by using the array_values() function in PHP. Here is an example that demonstrates how to convert an associative array into an indexed array:

$associative_array = array(
    'apple' => 10,
    'banana' => 20,
    'orange' => 30
);

$indexed_array = array_values($associative_array);

print_r($indexed_array);

In the above code, we have created an associative array containing three fruits and their corresponding prices. Next, we convert the associative array into an indexed array using the array_values() function. Finally, we use the print_r() function to output the newly created index array. Running this code will output the following:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
)

In the above output, we can see that we have successfully created a new indexed array containing all the values ​​from the original associative array, but not key. This is because we used the array_values() function to extract all the values ​​from the associative array and store them in a new array.

  1. Merge index array and associative array

In addition to converting index array and associative array separately, you can also merge the two arrays into a new array. In this case, the keys of the associative array will be used as the keys of the indexed array. Here is an example that demonstrates how to merge an index array and an associative array into a new array:

$index_array = array('apple', 'banana', 'orange');
$associative_array = array(
    'apple' => 10,
    'banana' => 20,
    'orange' => 30
);

$merged_array = array_merge($index_array, $associative_array);

print_r($merged_array);

In the above code, we first create an index array $index_array and an associative array $associative_array. Next, we use the array_merge() function to merge these two arrays into a new array $merged_array. Finally, we use the print_r() function to output the newly created array. Running this code will output the following result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [apple] => 10
    [banana] => 20
    [orange] => 30
)

In the above output, we can see that we have successfully merged the index array and the associative array into a new array, and this array contains the original All values ​​and keys in the array.

Summary:

In PHP, index arrays and associative arrays are very important data types. Sometimes, we need to convert an array of one type to an array of another type, or merge them into a new array. No matter what type of array you need, there are several ways to do it in PHP. This article provides some basic code examples to help you get started with arrays in PHP.

The above is the detailed content of Conversion of php index and associative 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