第 8 章 使用数组
8.2 创建数组
<?php
$rainbow = array('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet');
echo $rainbow[0];
?>
结果:
red
<?php
$rainbow[] = 'red';
$rainbow[] = 'orange';
$rainbow[] = 'yellow';
$rainbow[] = 'green';
$rainbow[] = 'blue';
$rainbow[] = 'indigo';
$rainbow[] = 'violet';
echo $rainbow[1];
?>
结果:
orange
<?php
$rainbow[0] = 'red';
$rainbow[1] = 'orange';
$rainbow[2] = 'yellow';
$rainbow[3] = 'green';
$rainbow[4] = 'blue';
$rainbow[5] = 'indigo';
$rainbow[6] = 'violet';
echo $rainbow[2];
?>
结果:
yellow
8.2.1 创建关联数组
<?php
$character = array("name" => "Bob",
"occupation" => "superhero",
"age" => 30,
"special power" => "x-ray vision");
echo $character['occupation'];
?>
结果:
superhero