1、PHP如何获取数组里元素的个数实例
在 PHP 中,使用 count()函数对数组中的元素个数进行统计。
例如,使用 count()函数统计数组元素的个数,示例代码如下:
<"Content-Type:text/html; charset=utf-8");
$arr = array("php","thinkphp","laravel");
echo count($arr);
输出结果为:
3
下面的一个实例将课程数据存放在数组中,使用 count()函数递归地统计数组中数量并输出,具体代码如下:
<"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。
下面实例综合应用数组函数,实现更新数组中的元素的值,具体示例代码如下:
<"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;
}
"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>
<"#" method="post" name="form-<">
<tr>
<td width="145" align="center" bgcolor="#fff"><"145" align="center" bgcolor="#fff"><"145" align="center" bgcolor="#fff">
<input type="text" name="count" id="count" value="<" size="4">
<input type="hidden" name="name" id="name" value="<">
<input type="submit" type="submit" value="更改">
</td>
<td width="145" align="center" bgcolor="#fff"><"3"><"text-align: center">
说明:array_search()函数最常见的应用是购物车,实现对购物车中指定商品数量的修改和删除!
3、PHP一维数组与二维数组相互转换的示例
一维数组转换二维数组的示例代码:
<"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);
输出的结果为:
二维数组转为一维数组的方法:
<"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');
<"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中数组怎么循环输出"htmlcode">
<"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>
<"submit"){
echo "$name=$value<br/>";
}
}
运行结果如下图所示:
说明:
each()函数用于返回当前指针位置的数组值,同时将指针推进到下一个位置。返回的数组包含4个键,键 0 和 key 包含键名,而键 1 和 value 包含相应的数据。如果程序在执行 each()函数时指针已经位于数组末尾,则返回 false。
5、PHP数组与字符串相互转换
1.使用 explode()函数将字符串转换成数组
<"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>
<"*",$content);
while(list($name,$value) = each($data)){
echo '<input type="checkbox" name="checkbox" value="checkbox">';
echo $value."\n";
}
}
运行结果如下
2.使用 implode()函数将数组转换成一个字符串
<"htmlcode">
标签:
PHP,数组,php使用数组
明霞山资源网 Design By www.htccd.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。







