1、PHP如何獲取陣列里元素的個數實體
在 PHP 中,使用 count()函式對陣列中的元素個數進行統計,
例如,使用 count()函式統計陣列元素的個數,示例代碼如下:
<?php header("Content-Type:text/html; charset=utf-8"); $arr = array("php","thinkphp","laravel"); echo count($arr);
輸出結果為:

下面的一個實體將課程資料存放在陣列中,使用 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);
輸出結果為:

注意:在統計二維陣列時,如果直接使用 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="https://www.cnblogs.com/chenyingying0/p/<?php echo $arrcount[$i]; ?>" size="4"> <input type="hidden" name="name" id="name" value="https://www.cnblogs.com/chenyingying0/p/<?php echo $arrname[$i]; ?>"> <input type="submit" type="submit" value="https://www.cnblogs.com/chenyingying0/p/更改"> </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/>'; }
遍歷結果為:

第二種: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="https://www.cnblogs.com/chenyingying0/p/登錄">
</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="https://www.cnblogs.com/chenyingying0/p/提交">
</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="https://www.cnblogs.com/chenyingying0/p/checkbox">';
echo $value."\n";
}
}
運行結果如下


2.使用 implode()函式將陣列轉換成一個字串
<?php $arr = array(1,2,3,4); $str = implode($arr); echo $str;
輸出結果為:

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);

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/40929.html
標籤:PHP
上一篇:Windows 服務器上的 WordPress 站點優化筆記
下一篇:PHP陣列的排序
