How to write two-dimensional array in php

PHPz
Release: 2023-04-17 14:36:18
Original
568 people have browsed it

PHP is a widely used back-end programming language. Its flexibility and easy-to-use features make it widely popular in development. In PHP, array is an important data type, and PHP provides a variety of ways to operate arrays. Among them, two-dimensional arrays are what we often use. They can store more complex and multi-dimensional data. So, how to write two-dimensional arrays correctly in PHP? The following article will introduce it to you in detail.

1. Define a two-dimensional array

When defining a two-dimensional array, we can use two methods, namely using the array() function and using []. The details are as follows:

  1. Use array() function
$array = array(
               array(1,2,3),
               array('a','b','c'),
             );
Copy after login

2. Use []

$array = [
          [1,2,3],
          ['a','b','c']
         ];
Copy after login

2. Access two-dimensional array elements

In PHP, we can use two indexes to access elements in the two-dimensional array, as follows:

$array = [
          [1,2,3],
          ['a','b','c']
         ];
echo $array[0][0];//访问第一个元素,结果为1
echo $array[1][2];//访问第二个元素的第三个元素,结果为c
Copy after login

3. Traverse the two-dimensional array

In PHP, we can use The foreach statement is used to traverse the two-dimensional array, as follows:

$array = [
          [1,2,3],
          ['a','b','c']
         ];
foreach($array as $row){
    foreach($row as $col){
        echo $col." ";
    }
    echo "<br />";
}
Copy after login

In the above code, the first foreach statement is used to traverse each row, and the second foreach statement is used to traverse each element.

4. Adding and deleting two-dimensional array elements

In PHP, we can use array_push(), array_pop(), array_unshift() and array_shift() functions to add or delete two-dimensional Array elements. The details are as follows:

1. Add elements

$array = [
          [1,2,3],
          ['a','b','c']
         ];
array_push($array[0], 4);//往第一个元素的末尾添加一个元素
array_unshift($array[1], 'd');//往第二个元素的开头添加一个元素
Copy after login

2. Delete elements

$array = [
          [1,2,3],
          ['a','b','c']
         ];
array_pop($array[0]);//删除第一个元素的末尾元素
array_shift($array[1]);//删除第二个元素的开头元素
Copy after login

5. Summary

Two-dimensional arrays are widely used in PHP . In this article, we provide a detailed introduction to how to define, access, traverse, add and delete two-dimensional array elements. I hope this article can help everyone master two-dimensional arrays in PHP.

The above is the detailed content of How to write 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!