Home Backend Development PHP Problem php set array mode

php set array mode

May 06, 2023 am 10:16 AM

PHP is a server-side scripting language that is widely used in Web server-side programming. In PHP, array is an important data structure. We can create and operate arrays in many ways. This article will introduce one of them: setting the array through key-value pairs.

1. Create an array

  1. Use [] or array() directly to create an empty array:
$a = [];
$b = array();
Copy after login
  1. At the same time when creating To set array elements, you can use subscripts or key-value pairs:
$a = [1, 2, 3];
$b = ['name' => '张三', 'age' => 20];
Copy after login
  1. Array elements do not have to be set in order, and the subscripts can also be strings:
$a = [1, 'name' => '张三', 2, 'age' => 20];
Copy after login

2. Set the array elements

  1. Set by subscript:
$a = [];
$a[0] = 1;
$a[1] = 'hello';
$a[2] = ['a' => 1, 'b' => 2];
Copy after login
  1. Set by key-value pair:
$a = ['name' => '张三', 'age' => 20];
$a['address'] = '北京市朝阳区';
Copy after login
  1. Set through special key-value pairs:
$a = [];
$a[] = 1; // 等价于 $a[0] = 1;
$a[] = 'hello'; // 等价于 $a[1] = 'hello';
$a['name'] = '张三'; // 等价于 $a['name'] = '张三';
Copy after login

3. Access array elements

  1. Access through subscripts:
$a = [1, 'hello', ['a' => 1, 'b' => 2]];
echo $a[0]; // 输出1
echo $a[1]; // 输出hello
echo $a[2]['a']; // 输出1
Copy after login
  1. Access through key-value pairs:
$a = ['name' => '张三', 'age' => 20];
echo $a['name']; // 输出张三
echo $a['age']; // 输出20
Copy after login

4. Traverse the array elements

  1. Use a for loop to traverse:
$a = [1, 2, 3];
for ($i = 0; $i < count($a); $i++) {
    echo $a[$i], ' ';
    // 输出:1 2 3
}
Copy after login
  1. Use a foreach loop to traverse:
$a = ['name' => '张三', 'age' => 20];
foreach ($a as $key => $value) {
    echo $key, ': ', $value, PHP_EOL;
    // 输出:name: 张三,age: 20
}
Copy after login
  1. Use a while loop to traverse:
$a = [1, 2, 3];
while (list($key, $value) = each($a)) {
    echo $key, ': ', $value, PHP_EOL;
    // 输出:0: 1,1: 2,2: 3
}
Copy after login

The above is the relevant content of setting the array through key-value pairs. I hope to be helpful. PHP provides a variety of ways to create and operate arrays. Only by mastering them can you better write efficient PHP programs.

The above is the detailed content of php set array mode. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)