php基础2

1,数组

数组在单个变量中存储多个值:

<?php

$cars=array("porsche","BMW","Volvo");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>

array() 函数用于创建数组:

有三种数组类型:

1,索引数组 - 带有数字索引的数组

索引是自动分配的(索引从 0 开始):

$cars=array("porsche","BMW","Volvo");

或者也可以手动分配索引:

$cars[0]="porsche";

$cars[1]="BMW";

$cars[2]="Volvo";

count() 函数用于返回数组的长度(元素数)

<?php

$cars=array("porsche","BMW","Volvo");

echo count($cars);

?>

输出3

遍历索引数组

如需遍历并输出索引数组的所有值,您可以使用 for 循环,就像这样:

实例

<?php

$cars=array("porsche","BMW","Volvo");

$arrlength=count($cars);  //得到3

for($x=0;$x<$arrlength;$x++) {

  echo $cars[$x];

  echo "<br>";

}

?>


 

2,关联数组 - 带有指定键的数组

指定键随意分配

<?php

$age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47");

echo "Elon is " . $age['Elon'] . " years old.";

?>

输出 Elon is 47 years old

遍历并输出关联数组的所有值,使用 foreach 循环

<?php

$age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47");

foreach($age as $x=>$x_value) {     //$x是键   $x_value是值

  echo "Key=" . $x . ", Value=" . $x_value;

  echo "<br>";

}

?>

Key=Bill, Value=63

Key=Steve, Value=56

Key=Elon, Value=47

3,多维数组 - 包含一个或多个数组的数组

高级里面的


 

数组的排序函数

PHP 数组排序函数:

sort() - 以升序对数组排序

rsort() - 以降序对数组排序

asort() - 根据值,以升序对关联数组进行排序

ksort() - 根据键,以升序对关联数组进行排序

arsort() - 根据值,以降序对关联数组进行排序

krsort() - 根据键,以降序对关联数组进行排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值