How to make an array start counting from non-0 in php

PHPz
Release: 2023-04-18 14:31:40
Original
718 people have browsed it

In PHP language, array is a commonly used data type. By default, PHP array subscripts start counting from 0, which is reasonable in most cases. However, sometimes we need to modify the starting point of counting array subscripts so that it starts from 1 or other values.

There are two ways in PHP to count arrays starting from non-0: one is to create an associative array and manually specify the key name of each array element, the other is to use PHP's built-in function to convert Index array.

The following are detailed descriptions of the two implementation methods:

Method 1: Manually specify the key name

When creating an array in PHP, if the key name is not specified, PHP Key names will be assigned auto-increasing numeric index values ​​by default, starting at 0. If we want the key name of the first element of the array to be 1, for example, an array containing 3 elements with key names 1, 2 and 3, then we can manually specify the key name as follows:

$array = array(
    1 => "first element",
    2 => "second element",
    3 => "third element"
);
Copy after login

In this method, we only need to specify the key names of the array elements 1, 2 and 3 instead of letting PHP assign the key names by itself. In this way, the subscripts of the array can start from 1.

This method is more suitable when we know in advance which elements are in the array, or we need to insert elements at a specific position.

Method 2: Use PHP built-in functions to convert indexed arrays

Another way to count the array starting from non-0 is to use PHP's built-in function array_values(). This function returns a new array of all values ​​in the array, with indexes starting from 0. Therefore, we can use this function to convert the index array and renumber its subscripts.

For example, we have an array containing 3 elements like this:

$array = array(
    "first element",
    "second element",
    "third element"
);
Copy after login

Now, we want the key name of the first element of the array to be 1, so we need to lower it Renumber the mark. You can use the following code:

$new_array = array_values($array);
$new_array = array_merge(array(1 => NULL), $new_array);
Copy after login

In this example, we pass the old array to the array_values() function, which will return a new indexed array with the subscripts counting exactly from 0. We then used the array_merge() function to merge the new array with the old array, and specified the key name of the first item as 1. At this time, the subscript of the new array starts from 1.

It should be noted that the key name of the first item in the array still starts from 0, but now we can access it through the new array, and the subscript of the new array starts counting from 1.

Summary:

No matter what situation you want to make the array start counting from non-0, PHP provides a solution. You can create an associative array and manually assign a key to each element, or use the built-in function array_values() to convert an indexed array.

However, before modifying the starting point of an array's subscripts, you should first consider whether you really need to do so. After all, it makes perfect sense for PHP to use zero-based subscripts by default, and many PHP programmers are used to it.

The above is the detailed content of How to make an array start counting from non-0 in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template