好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 如何在PHP中使用數組

如何在PHP中使用數組

熱門標簽:公司外呼系統中心 廈門400電話辦理選易號網 臨沂crm外呼系統平臺 天客通地圖標注 電子地圖標注怎么修改 地圖標注符號樣式有 菏澤語音外呼系統運營商 梧州市機器人外呼系統怎么樣 如何在世界地圖標注

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中數組最簡單的使用方法

標簽:黃石 郴州 貴陽 雞西 迪慶 白城 綿陽 瀘州

巨人網絡通訊聲明:本文標題《如何在PHP中使用數組》,本文關鍵詞  如,何在,PHP,中,使用,數組,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何在PHP中使用數組》相關的同類信息!
  • 本頁收集關于如何在PHP中使用數組的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 新婚夜被别人破了处| 影音先锋AV啪啪资源| 吻戏激烈摸全身床戏| 伊人亚洲大杳蕉色无码老司机| 天天射狠狠干| ??禁漫天堂??羞羞漫画在线看| 2021最新韩国理论三级0k| 《不戴套的瑜伽教练3》在线观看 亚洲AV天堂精品一区天堂霜花店 好爽?好紧?军人H男男视频 | 滴着奶水做着爱A片无码| 拍拍拍交性免费视频| bdsm中国精品调教chinese| 端庄美艳教师的沉沦1?12赵毅| 年轻的女学生4中字| 男强互攻互c高H| 双性白嫩美人哭唧唧h| 久久99精品综合国产首页| 色欲AV精品亚洲AV高清茉莉| 国产扒开腿精品无码高潮视频| c了英语老师一节课| 嗯啊好爽视频| 日本三人交xxx69| video18娇小粉嫩极品hd| www.欧美色| 精品18R3D动漫mmd在线| dy888午夜91精品国产| 美女露出胸让男人亲| 抱着娇妻被粗大陌生人玩3p| 中文字幕亚洲欧美日韩高清| 伊人思思久99久女女精品视频| 国产嫖妓一区二区三区AV视频 | 国产一线二线三线女装品牌大全| 精品国产又爽又爽www张津瑜 | 果冻传媒精选麻豆二区| 欧美高h视频| 精品3D肉动漫A片在线| swag国产在线| 我和麻麻在车后排乱小说| 日本乱理伦片在线观看一级| 欧美三级黄色| 日产精品高潮一区二区三区5月| 久久久久久久久福利|