Home  >  Article  >  Backend Development  >  How to make array assignment start from 1 in php

How to make array assignment start from 1 in php

PHPz
PHPzOriginal
2023-04-27 09:08:03566browse

PHP is a popular programming language that is widely used in production environments. Arrays are an important data type in PHP. I believe everyone will encounter a problem when learning PHP: array assignment starts from 0. How to make array assignment start from 1? This article explains how to implement this functionality in PHP.

Why does PHP array assignment start from 0?

Before explaining how to make PHP array assignment start from 1, let’s first understand why PHP array assignment starts from 0.

In fact, the starting index value of the PHP array can be changed. You can modify the starting value of the array index by modifying the array.start_index configuration item in php.ini. But why do PHP arrays start from 0 in most cases?

This is because most programming languages ​​assign array indexes starting from 0. This convention has been formed as early as the birth of computer science, because computer memory was very expensive at that time, and the index of the array started from 0 to minimize the memory space occupied by the array. In addition, starting from 0 makes it easier to process array data.

In addition, PHP assigns array indexes starting from 0, which can make PHP better compatible with other programming languages. This is also one of the reasons why PHP chooses to assign values ​​starting from 0.

Let PHP array assignment start from 1

Now that we have understood why PHP array assignment starts from 0, let’s look at how to make PHP array assignment start from 1.

In fact, it is very simple to implement this function. You only need to use the array_combine() function or the array_pad() function in PHP to achieve the goal.

  1. Usearray_combine() Function

array_combine() The function is used to create a new array, which uses The value of one array is used as its key name, and the value of the other array is used as its value. The specific usage is as follows:

array_combine(array $keys, array $values): array

Among them, the $keys parameter is used as the key name of the new array, and the $values parameter is used as the value of the new array.

We can use this function to create an array starting from 1. The code example is as follows:

<?php
// 从 1 开始赋值的数组
$array = array_combine(range(1, count($array)), $array);
?>

Among them, the range() function is used to create an array containing the specified range. Array of inner elements. Here we create an array from 1 to the length of the array, use it as the key of the new array, and then use the value of the original array as the value of the new array. This will return a new array starting at 1.

  1. Using array_pad() Function

array_pad() The function is used to fill an array to a specified length. If the padding length is greater than the array length, the array is filled with the specified value, otherwise no padding is performed. The specific usage is as follows:

array_pad(array $array, int $length, mixed $value): array

Among them, the $array parameter is used to specify the array to be filled, the $length parameter is used to specify the length of the array after filling, $value The parameter is used to specify the filled value.

We can use this function to assign values ​​to the PHP array starting from 1. The code example is as follows:

<?php
// 从 1 开始赋值的数组
$array = array_pad($array, count($array) + 1, &#39;&#39;);
unset($array[0]);
?>

Among them, we use the unset() function to remove the new The first element of the array, because when using the array_pad() function, if the length of the filled array is less than the filling length, the first element will be filled with the specified value. Therefore, we need to remove it to get a new array starting from 1.

Summary

So far, we have understood why PHP arrays are assigned values ​​starting from 0, and how to make PHP arrays assigned values ​​starting from 1. I believe you have mastered this technique and can freely assign values ​​to PHP arrays from a custom position.

In actual development, if we need to start assigning values ​​from a specified location, we can choose the appropriate method according to our needs. No matter which method is used, it makes it easier for us to process arrays.

The above is the detailed content of How to make array assignment start from 1 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