Home > Backend Development > PHP Problem > PHP array subscript variable?

PHP array subscript variable?

PHPz
Release: 2023-05-06 11:15:07
Original
407 people have browsed it

PHP is a very flexible programming language that supports a variety of data types and data structures, among which arrays are a very common data structure. In PHP, arrays can contain elements of different types, from simple integers and strings to objects and other arrays, making them ideal tools for handling large amounts of data.

In PHP, array values ​​can use numeric indexes or string keys. When using numeric indexes, each value has a unique numeric identifier. For example, the following code defines an array named "$fruits" that contains three fruits, identified using numeric indices 0, 1, and 2:

$fruits = array("apple", "banana", "cherry");
Copy after login

When using string keys, each Each value is identified by a string. For example, the following code defines an array named "$person" that contains a person's name, age, and address, each of which is identified using the string keys "name", "age", and "address":

$person = array(
    "name" => "John Doe",
    "age" => 35,
    "address" => "123 Main Street"
);
Copy after login

In PHP, both the keys and values ​​of an array can be variables. This means you can set them dynamically at runtime. For example, the following code shows how to use variables as keys of an array:

$fruit1 = "apple";
$fruit2 = "banana";
$fruit3 = "cherry";

$fruits = array(
    $fruit1 => "red",
    $fruit2 => "yellow",
    $fruit3 => "red"
);

echo $fruits[$fruit1]; // 输出“red”
Copy after login

In this example, we first define three variables, which contain the names of three fruits. We then created an array called "$fruits" and assigned the color of each fruit to them individually using variables as keys. Finally, we output a value in the array using the variable "$fruit1" as an index, which results in "red".

Similarly, you can also use variables as the values ​​of the array as follows:

$person = array(
    "name" => "John Doe",
    "age" => 35,
    "address" => "123 Main Street"
);

$fieldName = "age";
echo $person[$fieldName]; // 输出“35”
Copy after login

In this example, we first define an array named "$person", which contains Some information about a person. Then, we define a variable "$fieldName" that contains a string "age". Finally, we output the value in the array using the variable "$fieldName" as the index, and the output is "35".

Overall, arrays in PHP are flexible and using variables as keys or values ​​is not a problem, which is a big advantage of this data structure.

The above is the detailed content of PHP array subscript variable?. 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