How to query and assign values ​​to a two-dimensional array in PHP

PHPz
Release: 2023-04-27 09:31:01
Original
468 people have browsed it

PHP is a widely used scripting language that is used for many web development tasks, especially in building dynamic web applications. Among them, two-dimensional array is a common data structure in PHP and one of the data types that often needs to be processed in development.

In PHP, a two-dimensional array is actually an array containing multiple arrays. Each subarray can contain multiple key-value pairs, each key-value pair representing a data item. By using multidimensional arrays, we can organize, store and query data more conveniently. This article will introduce how to create, operate and query two-dimensional arrays in PHP.

Creating a two-dimensional array

Creating a two-dimensional array in PHP is very simple. We can use the array() function to create a multidimensional array. The following is a sample code to create a two-dimensional array:

$student_scores = array(
array("Zhang San", 85, 78, 93),
array("李思", 75 , 80, 88),
array ("王五", 90, 91, 87)
);

In the above code, we create a two-dimensional array containing three sub-arrays Array $student_scores. Each subarray contains four elements, namely the student's name and three test scores.

Querying a two-dimensional array

Querying a two-dimensional array is one of the most common operations in PHP development. We can access elements in a 2D array by using two different indices. The first index represents the index of the subarray to be accessed, while the second index represents the position in the subarray of the element to be found.

For example, we can use the following code to access the third score of the second subarray in the $student_scores array (that is, John's score).

echo $student_scores1; // Output: 88

Use a loop to traverse the array

Usually, we need to use a loop to traverse the two-dimensional array so that when the length of the array is not known, access each element in it. Here we introduce two commonly used loop methods:

  1. for loop to traverse the array

Using a for loop to traverse the array is one of the most common methods, which allows us to easily Traverse all elements in a two-dimensional array. The following is an example of a basic for loop traversing an array:

for ($i=0; $i for ($j=0; $j< ;count($student_scores[$i]); $j ) {

  echo $student_scores[$i][$j]. " ";
Copy after login

}
echo "\n";
}

In this code, we use Two loops to iterate through all elements in the $student_scores array. The first loop variable $i represents the index of the array to be accessed, while the second loop variable $j represents the index of the element in the subarray to be accessed.

  1. foreach loop traverses the array

In addition to the for loop, you can also use the foreach loop to traverse the array. When using a foreach loop to iterate over an array, we can iterate through each sub-array and do not need to know the length of the array. The following is an example of using foreach to traverse an array:

foreach ($student_scores as $scores) {
foreach ($scores as $score) {

  echo $score. " ";
Copy after login

}
echo "\ n";
}

In this code, we define a variable named $scores to get each sub-array in the array, and use the variable $score to get each sub-array. elements.

Updating elements in a two-dimensional array

Updating elements in a two-dimensional array is another common operation. We can use the same way to access array elements and then replace the data items with new values. The following is an example of updating an element in the $student_scores array:

$student_scores1 = 90;

In this example, we update John Doe’s third test score from 88 points is 90 points.

Summary

Two-dimensional array is a very common data type in PHP. We can use the array function array() to create a two-dimensional array and use two indices to access individual elements in the subarray. We can use a loop to iterate through the array and update the elements in the two-dimensional array. This article only introduces some basic concepts of two-dimensional arrays, hoping to help you better use two-dimensional arrays in PHP.

The above is the detailed content of How to query and assign values ​​to a two-dimensional array 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