Get the App
SLTechnology News&Howtos  ›  Development  › 

How does php modify the subscript of an array

Shulou Source: shulou.com Published: 2022-05-31 16:35:14 10月02日 Update

This article mainly explains the "php how to modify the array subscript", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php how to modify the array subscript" bar!

Two methods: 1. Using the "array_values ($arr)" statement, you can convert an associative array into an indexed array and a string subscript into a pure numeric subscript. 2, use "array_combine (key name array, original array)" statement, the elements of one array as the subscript of another array, the number of elements of the two arrays should be the same.

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

Two methods of modifying Array subscript by php

1. Use the array_values () function

The array_values () function converts an associative array into an indexed array and converts a string subscript to a pure numeric subscript.

2. Use the array_combine () function

The array_combine () function can take the elements of one array as the key name (subscript) of another array.

Example 1: convert an indexed array to an associative array

Example 2: modify the subscript of an associative array

Description:

The array_combine ($keys,$values) function creates a new array by merging two arrays, where the element in the $keys array is the key name of the new array, and the element of the $values array is the key value of the new array.

It is important to note, however, that when creating an array using the array_combine () function, the number of elements in the $keys array and the $values array must be the same, so that the key name and key value correspond one to one, otherwise an error will be reported and FALSE will be returned.

And the $keys array cannot be a multi-dimensional array, otherwise an error will be reported, but the $values array can be a multi-dimensional array.

Thank you for your reading, the above is the content of "how to modify the subscript of the array by php". After the study of this article, I believe you have a deeper understanding of how to modify the subscript of the array by php, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Array subscript element function association index learning consistency number content character string number method example statement report multidimensional one-to-one correspondence two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Microsoft Linux MariaDB