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

How to convert subscript array to associative array in php

PHPz
PHPzOriginal
2023-04-19 11:40:22452browse

In PHP development, sometimes it is necessary to convert the subscript array into an associative array to facilitate some operations. This article will introduce how to use PHP to convert a subscript array into an associative array.

  1. array_combine function

The array_combine function is a function provided by PHP itself, which can combine two arrays into an associative array. We can use this function to convert a subscripted array into an associative array. An example is as follows:

<?php
$keys   = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;);
$values = array(1,   2,   3);

$result = array_combine($keys, $values);

print_r($result);
?>

The running results are as follows:

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

As can be seen from the above results, we successfully converted the original subscript array into an associative array. Among them, the $keys parameter represents the key name of the associative array, and the $values ​​parameter represents the key value of the associative array. If the number of elements in the two arrays is not the same, it will lead to incorrect results, so make sure that the number of elements in the two arrays is equal.

  1. Manual implementation

We can also manually convert the subscript array into an associative array. An example is as follows:

<?php
$numbers = array(1, 2, 3);
$letters = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;);

$combined_array = array();

foreach($numbers as $key=>$value) {
    $combined_array[$value] = $letters[$key];
}

print_r($combined_array);
?>

The running result is as follows:

Array
(
    [1] => a
    [2] => b
    [3] => c
)

By traversing the key values ​​and values ​​of the subscript array, and then using them as the key values ​​and values ​​of the associative array.

  1. Use the array_map function

In addition, you can also use the array_map function in PHP to convert the subscript array into an associative array. An example is as follows:

<?php
$numbers = array(1, 2, 3);
$letters = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;);

$combined_array = array_map(null, $numbers, $letters);

print_r($combined_array);
?>

The running results are as follows:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a
        )

    [1] => Array
        (
            [0] => 2
            [1] => b
        )

    [2] => Array
        (
            [0] => 3
            [1] => c
        )

)

As can be seen from the results, the array_map function returns a two-dimensional array, which needs to be further converted into an associative array.

The above are several methods for converting PHP subscript arrays to associative arrays. No matter which method is used, it should be noted that the number of elements in the two arrays is equal, otherwise it will lead to erroneous results.

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