Various methods of creating and traversing php arrays

王林
Release: 2023-05-19 21:44:09
Original
505 people have browsed it

In PHP development, array is a very important data type. It can store multiple elements and can dynamically expand and shrink in size at runtime. In this article, we will explore various methods of creating and traversing arrays in PHP.

1. Array creation

a. Direct creation

You can use the array() function to create a simple array. Inside the brackets we can list some elements, separated by commas. For example:

$myArray = array('苹果', '香蕉', '橘子', '葡萄');
Copy after login

At this time, myArray is a non-associative array containing four elements.

b. Associative array

Associative array is a collection of key-value pairs. This means that each element has a key and a corresponding value. To create an associative array in PHP, you can use the following syntax:

$myArray = array( 'name' => '小明', 'age' => 18, 'gender' => '男', );
Copy after login

In the above example, we can use a string as the key name and then use any value we want as the value.

2. Array traversal

a. for loop

It is very simple to use the for loop to traverse an array. First, we get the length of the array using the count() function and then iterate over each element in a for loop.

$myArray = array('苹果', '香蕉', '橘子', '葡萄'); $arrLength = count($myArray); for($i=0; $i<$arrLength; $i++) { echo $myArray[$i]; }
Copy after login

In the above example, $i is the iteration variable and $myArray[$i] is the element of the current iteration. The $i variable inside the square brackets is the index of the element in the array.

b. foreach loop

The foreach loop is the simplest way to iterate through each element in an array. The foreach loop has two parameters: one is the array to be traversed, and the other is the variable used to store the current element value.

$myArray = array('苹果', '香蕉', '橘子', '葡萄'); foreach($myArray as $value) { echo $value; }
Copy after login

In the above example, $value is the element value of the current iteration. When the code is executed, all element values in the array will be output in sequence.

c. Traversal of associative arrays

If you want to traverse an associative array, you need to use a foreach loop and another variable to store the key name.

$myArray = array( 'name' => '小明', 'age' => 18, 'gender' => '男', ); foreach($myArray as $key => $value) { echo "{$key}: {$value}"; }
Copy after login

In the above example, the variable used to store the key name is called $key, and $value is the element value of the current iteration. The code will output each key name and corresponding value in the array one by one.

d. while loop

The while loop is another option for traversing an array. When using an iteration variable in a while loop, you can access each element in the array by incrementing it.

$myArray = array('苹果', '香蕉', '橘子', '葡萄'); $arrLength = count($myArray); $i = 0; while($i < $arrLength) { echo $myArray[$i]; $i++; }
Copy after login

In the above example, we first get the length of the array through the count() function, and then traverse each element in the array one by one.

e. do-while loop

The do-while loop is very similar to the while loop. The only difference is that it performs an iteration condition check before running the loop body instead of at the first before a loop iteration.

$myArray = array('苹果', '香蕉', '橘子', '葡萄'); $arrLength = count($myArray); $i = 0; do { echo $myArray[$i]; $i++; } while($i < $arrLength);
Copy after login

In the above example, similar to the while loop, the code will gradually output the values of each element in the array.

Conclusion

In PHP development, array is a very important data type. Creating and traversing arrays is very common in everyday programming work. In this article, we covered the creation of arrays in PHP and the different ways to represent traversal of arrays, including for loops, foreach loops, while loops, and do-while loops. I hope these methods will help you in your work on array traversal.

The above is the detailed content of Various methods of creating and traversing php arrays. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!