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;$icount($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=$valuebr/>";
}
}
運行結果如下圖所示:

說明:
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數組實際占用內存大小原理解析
- PHP數組訪問常用方法解析
- PHP如何使用array_unshift()在數組開頭插入元素
- PHP數組Key強制類型轉換實現原理解析
- PHP讀取遠程txt文檔到數組并實現遍歷
- PHP基于array_unique實現二維數組去重
- php中數組最簡單的使用方法