Home >Backend Development >PHP Tutorial >PHP array introductory tutorial: how to create, traverse and manipulate arrays

PHP array introductory tutorial: how to create, traverse and manipulate arrays

王林
王林Original
2023-06-11 14:12:101426browse

PHP array is a powerful and flexible data structure. This article will introduce how to create, traverse and operate PHP arrays for beginners.

1. Create a PHP array

In PHP, an array is an ordered collection composed of a set of key-value pairs. You can create an array in the following two ways:

  1. Create an array directly and assign a key name to each element:
$array = array('name' => '小明', 'age' => 18, 'gender' =>'男');

In the above code, $array represents an array variable, which contains There are three elements, namely name, age and gender. Each element consists of a key name and a key value. The key name is represented by a string, and the key value can be any type of data.

  1. Use square brackets to add elements to the array one by one:
$array[0] = 'apple';
$array[1] = 'orange';
$array[2] = 'banana';

In the above code, the $array array contains three elements, They are 'apple', 'orange' and 'banana' respectively, and their key names are 0, 1 and 2 respectively.

2. Traverse PHP arrays

In PHP, we can use the foreach loop to traverse arrays. The example is as follows:

foreach ($array as $key => $value) {
    echo "键名:".$key.",键值:".$value."<br>";
}

In the above code, $array represents the array we want to traverse, $key represents the key name of the element currently looped to, $value represents the key value of the current element.

3. Manipulate PHP arrays

  1. Get array elements

You can use square brackets plus the key name of the element to get an element in the array The value of Change the value of the 'name' element in the array to '小红'.

  1. Delete array elements

Use the unset function to delete an element in the array. The example is as follows:

echo $array['name']; // 输出:小明

Add array element
  1. You can add elements to the array by direct assignment or using the array_push function. The example is as follows:
$array['name'] = '小红';

The above code adds a new element and a new element value to the array respectively. The key name of the first element is 'address' and the key value is 'Beijing City'. Since the second element does not display a specified key name, the system will automatically assign it a serial number as the key name.

    Summary
  1. Through this article, we learned the basic concepts, creation, traversal and operation methods of PHP arrays. With this knowledge in hand, we can use PHP arrays more flexibly.

The above is the detailed content of PHP array introductory tutorial: how to create, traverse and manipulate arrays. 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