如何在PHP中使用數(shù)組
1、PHP如何獲取數(shù)組里元素的個數(shù)實例
在 PHP 中,使用 count()函數(shù)對數(shù)組中的元素個數(shù)進行統(tǒng)計。
例如,使用 count()函數(shù)統(tǒng)計數(shù)組元素的個數(shù),示例代碼如下:
<?php header("Content-Type:text/html; charset=utf-8"); $arr = array("php","thinkphp","laravel"); echo count($arr);
輸出結(jié)果為:
3
下面的一個實例將課程數(shù)據(jù)存放在數(shù)組中,使用 count()函數(shù)遞歸地統(tǒng)計數(shù)組中數(shù)量并輸出,具體代碼如下:
<?php header("Content-Type:text/html; charset=utf-8"); $arr = array( "php"=>array("php","thinkphp","laravel"), "js"=>array("vue","react") ); echo count($arr,true);
輸出結(jié)果為:
7
注意:在統(tǒng)計二維數(shù)組時,如果直接使用 count()函數(shù)只會顯示到一維數(shù)組的個數(shù),所以使用遞歸的當(dāng)時來統(tǒng)計二維數(shù)組的個數(shù)!
2、PHP怎么查詢數(shù)組中的指定元素
array_search()函數(shù)在數(shù)組中搜索給定的值,找到后返回鍵值,否則返回 false 。在 PHP 4.2.0之前,函數(shù)在失敗時返回 null 而不是 false。
下面實例綜合應(yīng)用數(shù)組函數(shù),實現(xiàn)更新數(shù)組中的元素的值,具體示例代碼如下:
<?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); //獲取要更改的數(shù)據(jù)的鍵名 $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">數(shù)量</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()函數(shù)最常見的應(yīng)用是購物車,實現(xiàn)對購物車中指定商品數(shù)量的修改和刪除!
3、PHP一維數(shù)組與二維數(shù)組相互轉(zhuǎn)換的示例
一維數(shù)組轉(zhuǎn)換二維數(shù)組的示例代碼:
<?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);
輸出的結(jié)果為:
二維數(shù)組轉(zhuǎn)為一維數(shù)組的方法:
<?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);
結(jié)果為:
注意:array_column();可以有第三個參數(shù),如 $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);
結(jié)果為:
4、php中數(shù)組怎么循環(huán)輸出?遍歷數(shù)組的方法介紹
第一種:使用 foreach 結(jié)構(gòu)遍歷數(shù)組
<?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/>'; }
遍歷結(jié)果為:
php
thinkphp
laravel
第二種:list()函數(shù)遍歷數(shù)組
list()函數(shù)僅能用于數(shù)字索引且索引從 0 開始的數(shù)組
下面將通過具體實例講解 list()函數(shù)和 each()函數(shù)的綜合應(yīng)用,獲取儲存在組數(shù)中的用戶登錄信息。
具體開發(fā)步驟如下:
1.利用開發(fā)工具,新建一個PHP 動態(tài)頁,保存為index.php。
2.應(yīng)用 HTML 標(biāo)記設(shè)計頁面。首先創(chuàng)建用戶登錄表單,用于實現(xiàn)用戶登錄信息的錄入,然后使用 each()函數(shù)提取全局數(shù)組$_POST中的內(nèi)容,最后使用 white 語句循環(huán)輸出用戶所提交的注重信息。
示例代碼如下:
<!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/>"; } }
運行結(jié)果如下圖所示:
說明:
each()函數(shù)用于返回當(dāng)前指針位置的數(shù)組值,同時將指針推進到下一個位置。返回的數(shù)組包含4個鍵,鍵 0 和 key 包含鍵名,而鍵 1 和 value 包含相應(yīng)的數(shù)據(jù)。如果程序在執(zhí)行 each()函數(shù)時指針已經(jīng)位于數(shù)組末尾,則返回 false。
5、PHP數(shù)組與字符串相互轉(zhuǎn)換
1.使用 explode()函數(shù)將字符串轉(zhuǎn)換成數(shù)組
<?php header("Content-Type:text/html;charset=utf-8"); $str = "i am learning php"; $arr = explode(" ", $str); print_r($arr);
輸出結(jié)果為:
在開發(fā)一個投票管理系統(tǒng)時,經(jīng)常需要在后臺添加投票選項到投票系統(tǒng),以作為投票的內(nèi)容。下面使用 explode()函數(shù)對添加的投票選項通過“*”進行區(qū)分,然后使用 white 循環(huán)語句分別再也面中輸出添加的投票選項。
具體開發(fā)步驟如下:
(1)利用開發(fā)工具,新建一個 PHP 動態(tài)頁,保存為index.php。
(2)使用 HTML 標(biāo)記設(shè)計面,首先建立投票表單,用于實現(xiàn)添加投票選項,然后使用 each()函數(shù)提取全局數(shù)組$_POST 中的內(nèi)容,并最終使用 while 循環(huán)輸出投票選項內(nèi)容。代碼如下:
<!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"; } }
運行結(jié)果如下
2.使用 implode()函數(shù)將數(shù)組轉(zhuǎn)換成一個字符串
<?php $arr = array(1,2,3,4); $str = implode($arr); echo $str;
輸出結(jié)果為:
1234
6、PHP輸出數(shù)組-打印數(shù)組實例詳解
一般使用print_r來打印數(shù)組(當(dāng)然用var_dump也可以,但是結(jié)構(gòu)上不清晰)
<?php $arr = array(1,2,3,4); print_r($arr);
當(dāng)?shù)诙€參數(shù)為true時,print_r不會直接打印數(shù)組,而是將打印的內(nèi)容作為字符串返回
<?php $arr = array(1,2,3,4); echo print_r($arr,true);
以上就是如何在PHP中使用數(shù)組的詳細內(nèi)容,更多關(guān)于PHP使用數(shù)組的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
WordPress中用于獲取及自定義頭像圖片的PHP腳本詳解
這篇文章主要介紹了WordPress中用于獲取及自定義頭像圖片的PHP腳本編寫方法,分別為get_avatar()和alt標(biāo)簽的使用,需要的朋友可以參考下2015-12-12