>  기사  >  백엔드 개발  >  PHP 배열에 대한 몇 가지 기본 작업

PHP 배열에 대한 몇 가지 기본 작업

高洛峰
高洛峰원래의
2016-11-30 13:08:201051검색

코드는 다음과 같습니다.

<?php 
/* 
* 简单的数组定义与访问 
*/ 
echo "简单的数组定义与访问<br>"; 
echo "############################################################<br>"; 
$address=array(5); 
$address[0]="福州"; 
$address[1]="厦门"; 
$address[2]="漳州"; 
$address[3]="泉州"; 
$address[4]="宁德"; 
$address[5]="南平"; 
$address[6]="龙岩"; 
echo "我现在住在$address[1]<br>"; 
echo "############################################################<br><br><br>"; 
/* 
* 数组遍历 
*/ 
echo "通过for循环进行数组遍历<br>"; 
echo "############################################################<br>"; 
for($index=0;$index<count($address);$index++){ 
print("数组中第".$index."个的地区$address[$index]为<br>"); 
} 
echo "############################################################<br><br><br>"; 
/* 
* 数组初始化 
*/ 
echo "数组初始化,并通过日期函数得到当前月份的数字,输出相关数组下标的内容<br>"; 
echo "############################################################<br>"; 
$arrMonth=array("January","February","March","April","May","June","July","August","September","October","November","December"); 
date_default_timezone_set("utc"); //设置默认时区 
$month=date("m"); 
echo "数组结构为"; 
print_r($arrMonth); 
echo "当前是第".$month."月,他的英文是".$arrMonth[$month-1]."<br>"; 
echo "############################################################<br><br><br>"; 
/* 
*数组初始化,并定义键,然后通过键值访问数组 
*/ 
echo "数组初始化,并定义键,然后通过键访问数组<br>"; 
echo "############################################################<br>"; 
$arrMonth=array("Jan"=>"January","Feb"=>"February","Mar"=>"March","Apr"=>"April","May"=>"May","Jun"=>"June","Jul"=>"July" 
,"Aug"=>"August","Sept"=>"Septmber","Oct"=>"October","Nov"=>"November","Dec"=>"December" 
); 
echo "通过英文缩写Aug 访问数组".$arrMonth["Aug"]."<br>"; 
echo "############################################################<br><br><br>"; 
echo "下面通过Foreach遍历数组<br>"; 
echo "############################################################<br>"; 
foreach ($arrMonth as $key=>$value){ 
echo "  =>键是$key,值是$value<br>"; 
} 
echo "############################################################<br><br><br>"; 
/* 
* 定义多维数组 
*/ 
echo "定义二维数组<br>"; 
$arrArea=array("华东地区"=>array("福建","浙江"),"华北地区"=>array("北京","天津")); 
echo "华东地区=>".$arrArea["华东地区"][0] 
?>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.