您的位置:首页 > 编程语言 > PHP开发

如何在PHP中使用数组

2020-06-20 11:47 99 查看

1、PHP如何获取数组里元素的个数实例

在 PHP 中,使用 count()函数对数组中的元素个数进行统计。

例如,使用 count()函数统计数组元素的个数,示例代码如下:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr = array("php","thinkphp","laravel");
echo count($arr);

输出结果为:

3

下面的一个实例将课程数据存放在数组中,使用 count()函数递归地统计数组中数量并输出,具体代码如下:

<?php
header("Content-Type:text/html; charset=utf-8");
$arr = array(
"php"=>array("php","thinkphp","laravel"),
"js"=>array("vue","react")
);
echo count($arr,true);

输出结果为:

7

注意:在统计二维数组时,如果直接使用 count()函数只会显示到一维数组的个数,所以使用递归的当时来统计二维数组的个数!

2、PHP怎么查询数组中的指定元素

array_search()函数在数组中搜索给定的值,找到后返回键值,否则返回 false 。在 PHP 4.2.0之前,函数在失败时返回 null 而不是 false。

下面实例综合应用数组函数,实现更新数组中的元素的值,具体示例代码如下:

<?php
header("Content-Type:text/html; charset=utf-8");

$names = "手表1@手表2@手表3@手表4";
$prices = "111@222@333@444";
$counts = "1@3@5@2";
$arrname = explode("@",$names); //['手表1','手表2','手表3','手表4']
$arrprice = explode("@",$prices); //['111','222','333','444']
$arrcount = explode("@",$counts); //['1','3','5','2']

if($_POST){
$name = $_POST['name'];
$count = $_POST['count'];
$key= array_search($name, $arrname); //获取要更改的数据的键名
$arrcount[$key] = $count;
}

?>

<table width="500" birder="1" cellpadding="1" bordercolor="#fff" bgcolor="#c17e50">
<tr>
<td width="145" align="center" bgcolor="#fff">商品名</td>
<td width="145" align="center" bgcolor="#fff">单价</td>
<td width="145" align="center" bgcolor="#fff">数量</td>
<td width="145" align="center" bgcolor="#fff">总价</td>
</tr>
<?php
$sum = 0;
for($i=0;$i<count($arrname);$i++){
?>
<form action="#" method="post" name="form-<?php echo $i; ?>">
<tr>
<td width="145" align="center" bgcolor="#fff"><?php echo $arrname[$i]; ?></td>
<td width="145" align="center" bgcolor="#fff"><?php echo $arrprice[$i]; ?></td>
<td width="145" align="center" bgcolor="#fff">
<input type="text" name="count" id="count" value="<?php echo $arrcount[$i]; ?>" size="4">
<input type="hidden" name="name" id="name" value="<?php echo $arrname[$i]; ?>">
<input type="submit" type="submit" value="更改">
</td>
<td width="145" align="center" bgcolor="#fff"><?php echo $arrprice[$i]*$arrcount[$i]; ?></td>
</tr>
</form>
<?php
$sum += $arrprice[$i]*$arrcount[$i];;
}
?>
<tr>
<td>订单共计:</td>
<td colspan="3"><?php echo $sum; ?></td>
</tr>

</table>

说明:array_search()函数最常见的应用是购物车,实现对购物车中指定商品数量的修改和删除!

3、PHP一维数组与二维数组相互转换的示例

一维数组转换二维数组的示例代码:

<?php
header("Content-Type:text/html;charset=utf-8");

$arr[1] = array("a1","a2");
$arr[2] = array("b1","b2");

$newarr = array();
foreach($arr as $a){
$newarr[] = $a;
}
print_r($newarr);

输出的结果为:

二维数组转为一维数组的方法:

<?php
header("Content-Type:text/html;charset=utf-8");

$arr = array(
array(
'id'=>1,
'name'=>'cyy1'
),
array(
'id'=>2,
'name'=>'cyy2'
)
);

$ids = array_column($arr,'id');
$names = array_column($arr,'name');
print_r($names);

结果为:

注意:array_column();可以有第三个参数,如 $n = array_column($arr, 'name', 'id');

<?php
header("Content-Type:text/html;charset=utf-8");

$arr = array(
array(
'id'=>1,
'name'=>'cyy1'
),
array(
'id'=>2,
'name'=>'cyy2'
)
);

$names = array_column($arr,'name','id');
print_r($names);

结果为:

4、php中数组怎么循环输出?遍历数组的方法介绍

第一种:使用 foreach 结构遍历数组

<?php
header("Content-Type:text/html;charset=utf-8");

$arr = array(
'course1'=>'php',
'course2'=>'thinkphp',
'course3'=>'laravel',
);

foreach($arr as $k=>$v){
echo $v.'<br/>';
}

遍历结果为:

php

thinkphp

laravel

第二种:list()函数遍历数组

list()函数仅能用于数字索引且索引从 0 开始的数组

下面将通过具体实例讲解 list()函数和 each()函数的综合应用,获取储存在组数中的用户登录信息。

具体开发步骤如下:

1.利用开发工具,新建一个PHP 动态页,保存为index.php。

2.应用 HTML 标记设计页面。首先创建用户登录表单,用于实现用户登录信息的录入,然后使用 each()函数提取全局数组$_POST中的内容,最后使用 white 语句循环输出用户所提交的注重信息。

示例代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form name="form1" method="post">
<table width="323" border="1" cellpadding="1" cellspacing="1" bordercolor="#66cc33" bgcolor="#fff">
<tr>
<td width="118" height="24" bgcolor="#ccff33">用户名</td>
<td width="192" height="24" bgcolor="#ccff33">
<input type="text" name="username" id="username" size="24">
</td>
</tr>
<tr>
<td height="24" bgcolor="#ccff33">密  码</td>
<td height="24" bgcolor="#ccff33">
<input type="password" name="pwd" id="pwd" size="24">
</td>
</tr>
<tr>
<td height="24" colspan="2">
<input type="submit" name="submit" value="登录">
</td>
</tr>
</table>
</form>
</body>
</html>

<?php

while(list($name,$value)=each($_POST)){
if($name!="submit"){
echo "$name=$value<br/>";
}
}

运行结果如下图所示:

说明:

each()函数用于返回当前指针位置的数组值,同时将指针推进到下一个位置。返回的数组包含4个键,键 0 和 key 包含键名,而键 1 和 value 包含相应的数据。如果程序在执行 each()函数时指针已经位于数组末尾,则返回 false。

5、PHP数组与字符串相互转换

1.使用 explode()函数将字符串转换成数组

<?php
header("Content-Type:text/html;charset=utf-8");

$str = "i am learning php";
$arr = explode(" ", $str);
print_r($arr);

输出结果为:

在开发一个投票管理系统时,经常需要在后台添加投票选项到投票系统,以作为投票的内容。下面使用 explode()函数对添加的投票选项通过“*”进行区分,然后使用 white 循环语句分别再也面中输出添加的投票选项。

具体开发步骤如下:

(1)利用开发工具,新建一个 PHP 动态页,保存为index.php。

(2)使用 HTML 标记设计面,首先建立投票表单,用于实现添加投票选项,然后使用 each()函数提取全局数组$_POST 中的内容,并最终使用 while 循环输出投票选项内容。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="#" name="form1" method="post">
<table width="400" border="1" cellpadding="0" cellspacing="1" bordercolor="#ff9900" bgcolor="#ccff66">
<tr>
<td width="98" height="120">添加投票选项</td>
<td width="223" height="120">
<p>
<textarea name="content" id="content" cols="30" rows="5"></textarea><br>
<span>注意:每个选项用*分开</span>
</p>
</td>
<td width="61" height="120">
<input type="submit" name="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>

<?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";
}
}

运行结果如下

2.使用 implode()函数将数组转换成一个字符串

<?php
$arr = array(1,2,3,4);
$str = implode($arr);
echo $str;

输出结果为:

1234

6、PHP输出数组-打印数组实例详解

一般使用print_r来打印数组(当然用var_dump也可以,但是结构上不清晰)

<?php
$arr = array(1,2,3,4);
print_r($arr);

当第二个参数为true时,print_r不会直接打印数组,而是将打印的内容作为字符串返回

<?php
$arr = array(1,2,3,4);
echo print_r($arr,true);

以上就是如何在PHP中使用数组的详细内容,更多关于PHP使用数组的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP 数组