数组

1、数组的声明
数组的声明有两种方式:一种是使用array()函数声明数组,另一种是直接通过为数组元素赋值的方式声明。array()函数声明方式如下:

array array([mixed ...])

参数mixed的语法为key=>value,多个参数mixed间用都好分开,分别定义索引和值。索引可以是字符串或者数字。如果省略索引,则会自动产生0开始的整数索引,下一位则是目前最大索引+1.如果定义了两个完全一样的索引,则后者会覆盖前者。如果mixed是数组类型,则是二维数组。下标既可以是数值索引也可以是关联索引。使用array()函数定义数组是可以不用给出数组元素值而不给出键值。

<?php
	$array = array("jsp", "asp", "php");
	print_r($array);
?>

输出结果:

Array ( [0] => jsp [1] => asp [2] => php )

也可以这样声明:

<?php 
	$array = array('1' => "asp", '2' => "jsp", '3' => "php");
	print_r($array);
?>

输出结果:

Array ( [1] => asp [2] => jsp [3] => php )

直接为数组元素赋值是一种较为灵活的方法,如果在创建数组是不知道数组的具体长度,则使用这种方法较好。

<?php
	$array[1] = "我";
	$array[2] = "是";
	$array[3] = "你";
	$array[4] = "爸";
	$array[5] = "爸";
	print_r($array);
?>

输出结果:

Array ( [1] => 我 [2] => 是 [3] => 你 [4] => 爸 [5] => 爸 )

2、数组的类型
数字索引数组:数字索引一般表示数组元素在数组中的位置,由数字构成下标从0开始,若不特殊指定,则默认是从0开始,然后从此值自动增加。
关联索引数组:键名可以是数字和字符的组合,在一个数组中只要有一个键名不是纯数字,都被称为关联索引数组。
例如:

<?php
	$array = array('first' => 1, 'second' => 2, 'third' => 3);
	print_r($array);
?>

输出结果:

Array ( [first] => 1 [second] => 2 [third] => 3 )

3、二维数组
一个数组的元素如果是以为数组,则这个数组是二维数组。

<?php
	$array = array(
		"书籍" => array("1" => "文学", "2" => "哲学", "3" => "shift"),
		"体育" => array("1" => "跳远", "2" => "跳高"),
		"" => array("1" => "Apple", "2" => "Banana")
	);
	print_r($array);
?>

输出结果:

Array ( [书籍] => Array ( [1] => 文学 [2] => 哲学 [3] => shift ) [体育] => Array ( [1] => 跳远 [2] => 跳高 ) [] => Array ( [1] => Apple [2] => Banana ) )

4、遍历数组
foreach()函数遍历数组:
<?php
u r l = a r r a y ( &quot; 百 度 &quot; = &gt; &quot; w w w . b a i d . c o m &quot; , &quot; 编 程 体 验 网 &quot; = &gt; &quot; w w w . b c t y 365. c o m &quot; , &quot; 编 程 资 源 网 &quot; = &gt; &quot; w w w . b c 110. c o m &quot; ) ; f o r e a c h ( url = array(&quot;百度&quot; =&gt; &quot;www.baid.com&quot;, &quot;编程体验网&quot; =&gt; &quot;www.bcty365.com&quot;, &quot;编程资源网&quot; =&gt; &quot;www.bc110.com&quot;); foreach ( url=array(""=>"www.baid.com",""=>"www.bcty365.com",""=>"www.bc110.com");foreach(url as $key => $value) {
# code…
echo $value."
";
}
?>
输出结果:

www.baid.com
www.bcty365.com
www.bc110.com

**list()函数遍历数组:**语法格式是:void list(mixed ...)

<form name="form1" method="post">
	<table width="323" border="1" cellpadding="1" cellspacing="1" bordercolor="#66CC33" bgcolor="#FFFFFF">
		<tr>
			<td width="118" height="24" align="center" bgcolor="#CCFF33">用户名:</td>
			<td width="192" height="24" bgcolor="#CCFF33"><input type="text" name="user" class="inputcss" id="user" size="24"></td>
		</tr>
		<tr>
			<td height="24" align="center" bgcolor="#CCFF33">密&nbsp;&nbsp;码:</td>
			<td height="24" bgcolor="#CCFF3"><input type="password" name="pwd" class="inputcss" id="pwd" size="24"></td>
		</tr>
		<tr align="center" bgcolor="#CCFF33">
			<td height="24" colspan="2"><input type="submit" name="submit" value="登录"></td>
		</tr>
	</table>
</form>
<?php
	while (list($name,$value)=each($_POST)) {
		if ($name!="submit") {
			echo "$name=$value<br>";
		}
	}
?>

输出结果:
在这里插入图片描述
5、字符串与数组的转换
使用explode()函数将字符串转换成数组,语法格式:array explode(string separator, string string, [int limit])返回由字符串组成的数组,每个元素都是string的一个子串,他们被字符串separator作为边界点分隔出来。如果设置了limit参数,则返回最多limit个元素,而最后的那个元素包含string的剩余部分;如果separator是空字符(""),函数返回false,如果包含的值在string中找不到,则返回包含string单个元素的数组,如果参数是负数则返回除了最后-limit个元素外的所有元素。

<?php
	$str = "时装、休闲、职业装";
	$strs = explode("、", $str);
	print_r($strs)
?>

输出结果:

Array ( [0] => 时装 [1] => 休闲 [2] => 职业装 )

在开发投票系统时,后台经常需要替换选项

<form name="form1" method="post">
	<table width="400" border="1" cellpadding="0" cellspacing="1" bordercolor="#FF9900" bgcolor="#CCFF66">
		<tr align="center">
			<td width="98" height="120">添加投票选项:</td>
			<td width="223" height="120">
				<p><textarea name="content" cols="30" rows="5" id="content"></textarea>
					<br>
					<span class="style1">注意:每个选项间用*分隔</span></p>
			</td>
			<td width="61" height="120"><input type="submit" name="submit" value="提交"></td>
		</tr>
	</table>
</form>
<?php
	if ($_POST[submit]!="") {
		$content = $_POST[content];
		$data = explode("*", $content);
		while (list($name, $value)=each($data)) {
			echo '<input type="checkbox" name="checkbox" value="checkbox">';
			echo $value."\n";
		}
	}
?>

输出结果:
在这里插入图片描述
implode()函数将数组转化为字符串,语法格式:string implode(string glue, array pleces)glue是字符串类型,只要传入的分隔符;参数pieces是数组类型,指传入的要合并的数组变量名称
6、统计数组元素个数
使用count()函数可以对数组中元素个数进行统计,语法格式如下:int count(mixed array, [int mode])

array必要参数,输入的数组
mode可选参数,COUNT_RECURSIVE(或1),如果选中这个参数,将会递归对数组进行计算。对计算所谓数组的个数有用,默认值是0
<?php
	$array = array("函数参考大全", "PHP程序开发经典案例", "PHP5入门到精通");
	echo count($array);
?>

输出结果:

3

统计二维函数:

<?php
	$array = array("php" => array("PHP参考手册", "PHP真牛逼", "PHP是我的最爱"), "jsp" => array("jsp也牛逼"));
	echo count($array, 1);
?>

输出结果:

6

7、查询数组中指定的元素
array_search()函数,在数组中搜索给定的值,找到后返回键名,否则返回false,PHP4.2.0之前是返回null,语法格式如下:mixed array_search(mixed needle, array haystack, [bool strict])
needle:指定在数组中搜索的值;haystack:指定被搜索的数组;strict:可选参数,如果为true还将会检查给定值的类型。

<?php
	$name = "智能机器人@数码相机@天翼3G手机@瑞士手表";
	$price = "14998@2588@2666@66698";
	$counts = "1@2@3@4";
	$arrayid = explode("@", $name);
	$arraynum = explode("@", $price);
	$arraycount = explode("@", $counts);
	if ($_POST[submit]==true) {
		$id = $_POST[name];
		$num = $_POST[counts];
		$key = array_search($id, $arrayid);
		$arraycount[$key]=$num;
		$counts = implode("@", $arraycount);
	}
?>
<table width="580" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#c17e50">
	<tr>
		<td width="145" align="center" bgcolor="#FFFFFF" class="STYLE1">商品名称</td>
		<td width="145" align="center" bgcolor="#FFFFFF" class="STYLE1">价格</td>
		<td width="145" align="center" bgcolor="#FFFFFF" class="STYLE1">数量</td>
		<td width="145" align="center" bgcolor="#FFFFFF" class="STYLE1">金额</td>
	</tr>
	<?php
	for ($i=0; $i < count($arrayid); $i++) { 
	?>
	<form name="form1_<?php echo $i;?>" action="index03.php" method="post">
		<tr>
			<td height="25" align="center" bgcolor="#FFFFFF" class="STYLE2"><?php echo $arrayid[$i]; ?></td>
			<td align="center" bgcolor="#FFFFFF" class="STYLE2"><?php echo $arraynum[$i]; ?></td>
			<td align="center" bgcolor="#FFFFFF" class="STYLE2">
				<input type="text" name="counts" id="counts" value="<?php echo $arraycount[$i]; ?>" size="8">
				<input type="hidden" name="name" id="name" value="<?php echo $arrayid[$i]; ?>">
				<input type="submit" name="submit" value="更改">
			</td>
			<td align="center" class="STYLE2"><?php echo $arraycount[$i]*$arraynum[$i];?></td>
		</tr>>
	</form>
	<?php
	}
	?>
</table>

输出结果:
在这里插入图片描述
8、获取数组最后一个元素
通过函数array_pop()获取数组中的最后一个单元,并且将数组的长度减一,如果数组为空则返回null。语法格式:mixed array_pop(array array)

<?php
	$arr = array("asp", "jsp", "vb", "java");
	$array = array_pop(arr);
	echo $array."<br>";
	print_r($arr);
?>

输出结果:

java
Array ( [0] => asp [1] => jsp [2] => vb 

9、向数组中添加元素
使用函数array_push()向数组中添加元素,将数组当做一个栈,将传入的变量压入数组的末尾,数组的长度将增加变量的数目,返回数组新的单元总数。语法格式:int array_push(array array, mixed var, [mixed...])
array:是指定的数组;var:是压入数组的值。

<?php
	$array_push = array("PHP从入门到精通", "PHP范例手册");
	array_push($array_push, "PHP真牛逼", "PHP真棒");
	print_r($array_push);
?>

输出结果:

Array ( [0] => PHP从入门到精通 [1] => PHP范例手册 [2] => PHP真牛逼 [3] => PHP真棒 )

10、删除数组中的重复元素
使用array_unique()函数可以删除数组中的重复元素,将值作为字符串进行排序,然后对每个值只保留第一个键名,忽略所有后面的键名。语法格式:array array_unique(array array)

<?php
	$array_push = array("PHP从入门到精通", "PHP范例手册", "PHP牛逼", "jsp牛逼");
	array_push($array_push, "jsp牛逼", "asp牛逼");
	print_r($array_push);
	echo "<br>";
	$result = array_unique($array_push);
	print_r($result);
?>

输出结果:

Array ( [0] => PHP从入门到精通 [1] => PHP范例手册 [2] => PHP牛逼 [3] => jsp牛逼 [4] => jsp牛逼 [5] => asp牛逼 ) 
Array ( [0] => PHP从入门到精通 [1] => PHP范例手册 [2] => PHP牛逼 [3] => jsp牛逼 [5] => asp牛逼 )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值