php array writing method

PHPz
Release: 2023-05-07 11:24:07
Original
589 people have browsed it

PHP is a powerful server-side scripting language that is widely used in web development. In PHP, array is a very important data type that allows us to store multiple values in one variable. This article will introduce how to write arrays in PHP.

  1. Declare array variables

In PHP, array variables can be declared as follows:

// 声明一个空数组 $arr = array(); // 声明一个带有元素的数组 $arr = array('apple', 'orange', 'banana');
Copy after login

In addition, starting from PHP 5.4 version, also Array variables can be declared using simplified syntax:

// 空数组 $arr = []; // 带有元素的数组 $arr = ['apple', 'orange', 'banana'];
Copy after login
  1. Declaration of associative array

Associative array is a special type of array that allows us to Associated with a key so that the element can be accessed using that key. In PHP, an associative array is written as follows:

// 声明一个带有键的关联数组 $arr = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'male' );
Copy after login

You can also use simplified syntax to declare an associative array:

// 带有键的关联数组 $arr = [ 'name' => 'Tom', 'age' => 20, 'gender' => 'male' ];
Copy after login
  1. Access array elements

In PHP, you can use subscripts to access elements in an array. An array subscript is a numeric value or string that represents a specific element in the array. Array subscripts start counting from 0.

$arr = array('apple', 'orange', 'banana'); echo $arr[0]; // 输出 "apple" echo $arr[1]; // 输出 "orange" echo $arr[2]; // 输出 "banana" // 关联数组的访问方式 $info = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'male' ); echo $info['name']; // 输出 "Tom" echo $info['age']; // 输出 20 echo $info['gender']; // 输出 "male"
Copy after login
  1. Modify array elements

In PHP, you can modify the elements in the array through the array subscript.

$arr = array('apple', 'orange', 'banana'); $arr[1] = 'pear'; print_r($arr); // 输出 Array ( [0] => apple [1] => pear [2] => banana ) // 修改关联数组的值 $info = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'male' ); $info['name'] = 'Jerry'; print_r($info); // 输出 Array ( [name] => Jerry [age] => 20 [gender] => male )
Copy after login
  1. Traverse an array

In PHP, you can use a foreach loop to iterate through all elements in an array.

$arr = array('apple', 'orange', 'banana'); foreach ($arr as $value) { echo $value . ' '; // 输出 "apple orange banana " } // 遍历关联数组 $info = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'male' ); foreach ($info as $key => $value) { echo $key . ':' . $value . ' '; // 输出 "name:Tom age:20 gender:male " }
Copy after login

The above is how to write PHP arrays, I hope it can help everyone.

The above is the detailed content of php array writing method. 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!