主頁 > 知識庫 > 如何在PHP中使用數(shù)組

如何在PHP中使用數(shù)組

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

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

輸出結果為:

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

輸出結果為:

7

注意:在統(tǒng)計二維數(shù)組時,如果直接使用 count()函數(shù)只會顯示到一維數(shù)組的個數(shù),所以使用遞歸的當時來統(tǒng)計二維數(shù)組的個數(shù)!

2、PHP怎么查詢數(shù)組中的指定元素

array_search()函數(shù)在數(shù)組中搜索給定的值,找到后返回鍵值,否則返回 false 。在 PHP 4.2.0之前,函數(shù)在失敗時返回 null 而不是 false。

下面實例綜合應用數(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;$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()函數(shù)最常見的應用是購物車,實現(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);

輸出的結果為:

二維數(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);

結果為:

注意: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);

結果為:

4、php中數(shù)組怎么循環(huán)輸出?遍歷數(shù)組的方法介紹

第一種:使用 foreach 結構遍歷數(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/>';
}

遍歷結果為:

php

thinkphp

laravel

第二種:list()函數(shù)遍歷數(shù)組

list()函數(shù)僅能用于數(shù)字索引且索引從 0 開始的數(shù)組

下面將通過具體實例講解 list()函數(shù)和 each()函數(shù)的綜合應用,獲取儲存在組數(shù)中的用戶登錄信息。

具體開發(fā)步驟如下:

1.利用開發(fā)工具,新建一個PHP 動態(tài)頁,保存為index.php。

2.應用 HTML 標記設計頁面。首先創(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=$valuebr/>";
 }
}

運行結果如下圖所示:

說明:

each()函數(shù)用于返回當前指針位置的數(shù)組值,同時將指針推進到下一個位置。返回的數(shù)組包含4個鍵,鍵 0 和 key 包含鍵名,而鍵 1 和 value 包含相應的數(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);

輸出結果為:

在開發(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 標記設計面,首先建立投票表單,用于實現(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";
 }
}

運行結果如下

2.使用 implode()函數(shù)將數(shù)組轉(zhuǎn)換成一個字符串

?php
$arr = array(1,2,3,4);
$str = implode($arr);
echo $str;

輸出結果為:

1234

6、PHP輸出數(shù)組-打印數(shù)組實例詳解

一般使用print_r來打印數(shù)組(當然用var_dump也可以,但是結構上不清晰)

?php
$arr = array(1,2,3,4);
print_r($arr);

當?shù)诙€參數(shù)為true時,print_r不會直接打印數(shù)組,而是將打印的內(nèi)容作為字符串返回

?php
$arr = array(1,2,3,4);
echo print_r($arr,true);

以上就是如何在PHP中使用數(shù)組的詳細內(nèi)容,更多關于PHP使用數(shù)組的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • PHP數(shù)組實際占用內(nèi)存大小原理解析
  • PHP數(shù)組訪問常用方法解析
  • PHP如何使用array_unshift()在數(shù)組開頭插入元素
  • PHP數(shù)組Key強制類型轉(zhuǎn)換實現(xiàn)原理解析
  • PHP讀取遠程txt文檔到數(shù)組并實現(xiàn)遍歷
  • PHP基于array_unique實現(xiàn)二維數(shù)組去重
  • php中數(shù)組最簡單的使用方法

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

巨人網(wǎng)絡通訊聲明:本文標題《如何在PHP中使用數(shù)組》,本文關鍵詞  如,何在,PHP,中,使用,數(shù)組,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何在PHP中使用數(shù)組》相關的同類信息!
  • 本頁收集關于如何在PHP中使用數(shù)組的相關信息資訊供網(wǎng)民參考!
  • 推薦文章