You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.8 KiB
64 lines
1.8 KiB
<?php
|
|
//定义索引数组
|
|
$arr = array('a', 'b','c','hello');
|
|
echo '<pre>';
|
|
var_dump($arr);
|
|
|
|
//定义二维数组
|
|
$arr = array(
|
|
array('name'=>'邝','chinese'=>'90'),
|
|
array('name'=>'伟','chinese'=>'65')
|
|
);
|
|
echo '<pre>';
|
|
var_dump($arr);
|
|
echo $arr[0]['name'].'的分数是'.$arr[0]['chinese'];
|
|
|
|
echo '<br>';
|
|
|
|
//统计数组元素数量
|
|
$arr = array('a', 'b','c','hello');
|
|
$len = count($arr);
|
|
$len2 = sizeof($arr);
|
|
echo '$arr'.'的长度是'.$len.'<br>';
|
|
echo '$arr'.'的长度是'.$len2.'<br>';
|
|
|
|
//返回放弃数组指针指向的元素
|
|
$arr = array('a', 'b','c','hello');
|
|
$curVal = current($arr);
|
|
echo '当前的元素是'.$curVal.'<br>';
|
|
|
|
//获取当前的元素的键名
|
|
$arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞');
|
|
$curKey = key($arr);
|
|
echo '当前的键名是'.$curKey.'<br>';
|
|
|
|
//判断数组的键名或索引是否存在
|
|
$arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞');
|
|
if (array_key_exists('chinese',$arr)) {
|
|
echo '键名存在'.'<br>';
|
|
}else{
|
|
echo '键名不存在'.'<br>';
|
|
}
|
|
|
|
|
|
//判断数组中某个值是否存在
|
|
$arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞');
|
|
//判断'东莞'是否存在
|
|
$needle='东莞';
|
|
$ret=in_array($needle,$arr,true);
|
|
if ($ret==TRUE) {
|
|
echo "数组中包含$needle".'<br>';
|
|
}else{
|
|
echo "数组中不包含$needle".'<br>';
|
|
}
|
|
|
|
//数组合成一个字符串
|
|
$arr=array('name'=>'邝','chinese'=>'90','city'=>'东莞');
|
|
$str=implode($arr);
|
|
echo $str.'<br>';
|
|
|
|
//把数组中的值赋给一组变量
|
|
$arr=array('邝','90','东莞');
|
|
list($a,$b,$c) = $arr;
|
|
echo '$a = '.$a.'<br>'.'$b = '.$b.'<br>'.'$c = '.$c.'<br>';
|
|
?>
|