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

How to get array subscript in php

PHPz
PHPzOriginal
2023-04-20 09:07:27996browse

PHP is a very popular programming language suitable for developing web applications. In PHP, an array is just a data structure used to store data. When accessing array elements, use square brackets "[]" to get the value in the array. But what should we do when we need to get the array subscript? In this article, we will introduce some methods to get array subscript in PHP.

1. Use the array_keys() function

The array_keys() function is one of the common methods to obtain array subscripts in PHP. This function returns an array containing the keys of the original array. The following is a sample code:

<?php
$fruits = array("apple", "banana", "cherry");
$keys = array_keys($fruits);

print_r($keys);
?>

The output result of the above code is:

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

The above result indicates that the subscripts of the original array are 0, 1 and 2 respectively. It should be noted that if the key name of the array is a string instead of a number, the returned result will be a string.

2. Use foreach loop

Another way to get the array subscript is to use foreach loop. In each loop iteration, the key name of the array element can be accessed through the $key variable. The following is a sample code:

<?php
$fruits = array("apple", "banana", "cherry");

foreach ($fruits as $key => $value) {
    echo "下标为 " . $key . ", 值为 " . $value . "<br>";
}
?>

The output of the above code is:

下标为 0, 值为 apple
下标为 1, 值为 banana
下标为 2, 值为 cherry

With this method, we can get the subscript and value of the array element and perform some operations accordingly.

3. Use the array_keys() and array_values() functions

The array_keys() function can get the key array in the array, and the array_values() function can get the value array in the array. If you need to get the key name and value at the same time, you can use these two functions together. The following is a sample code:

<?php
$fruits = array("apple", "banana", "cherry");
$keys = array_keys($fruits);
$values = array_values($fruits);

$length = count($fruits);

for($i = 0; $i < $length; $i++) {
    echo "下标为 " . $keys[$i] . ", 值为 " . $values[$i] . "<br>";
}
?>

The output of the above code is the same as the output of the above foreach loop.

Through the above method, we can easily get the subscript in the PHP array. These methods not only help us better understand PHP arrays, but also help us obtain and process array data more easily when we develop web applications.

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