PHP Basics - Arrays

In PHP, an array is a special variable type that can store multiple values. There are several ways to create arrays in PHP, one of which is using the array() function.

1. Numeric array

Array with numeric ID keys

<?php
$scars = array("age","name","domicile"); // 使用数组函数创建一个空数组

# 人工分配ID键(或者说修改)
$scars[0] = "volvo"; // 使用索引为 0 的键创建一个元素值为 "volvo" 的数组元素
$scars[1] = "BMW"; // 使用索引为 1 的键创建一个元素值为 "BMW" 的数组元素
$scars[2] = "Toyota"; // 使用索引为 2 的键创建一个元素值为 "Toyota" 的数组元素

// 输出数组中的元素
echo "I like " . $scars[0] . ", " . $scars[1] . " and " . $scars[2] . ". \n";

// count 获取数组的长度(元素的个数)
echo "Count(scars): " . count($scars);
?>

2. Associative array

An associative array is a special type of array

Guess you like

Origin blog.csdn.net/weixin_43263566/article/details/134902404