PHP 數組長度

PHPz
發布: 2024-08-29 12:44:04
原創
220 人瀏覽過

PHP 數組長度被定義為一個數組,用於獲取其中的許多元素。使用 count() 函數和 size(),我們可以檢索元素的計數。陣列包含字串或整數值,可以是單維或多維的。此數組用於保存鍵值對中的值,這是一個索引數組,具有許多用於數組處理的內建函數。此處使用兩個 Right 函數(預先定義)可以節省大量計算值的時間。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

PHP 陣列長度定義如下:

Count(array name, mode);
登入後複製

此函數有兩個參數,即陣列名稱,後面跟著表示維度的模式。空數組表示零,對於非數組值返回“1”。

數組長度在 PHP 中如何運作?

陣列長度由函數的數量和大小決定。根據語法,可選的 mode 參數被遞歸地設定為 count(),這將遞歸地計算數組中的元素數量。此函數在多維數組中大量使用。

要知道陣列長度,我們有幾個常見的原因:

  • 使用「for」迴圈在元素之間移動。
  • 沒有回傳任何搜尋元素。
  • 計算數組中的平均值。

但在 PHP 中,為了取得陣列中元素的數量,這裡啟用了 sizeof 或 count 函數來預測 PHP 中的陣列長度。由於我們的程式碼中的元素數量會根據使用者需求而變化,因此了解陣列清單的實際長度非常重要。 PHP 有兩個內建函數,分別是 count 和 size of。

使用count():對元素進行計數。

我們可以這樣使用:

代碼:

$a1=array(6,3,1,9);
echo " The size is given as =", count($a1);
登入後複製

因此,在這裡,計數函數會傳回物件中的元素數量,並簡單地在關聯數組中進行計數。在上面的範例程式碼中,我們使用了一維數組。我們使用了 PHP 的原生函數 count,所以當我們執行上面的程式碼片段時,函數的輸出是「4」。這就是我們獲取值的方式。

第二種情況是當我們使用參數模式對元素進行計數時,為了執行此操作,我們傳遞了一個常數“recursive”作為參數來對元素進行計數。在這種情況下,數組的長度的確定方式不同。

代碼:

$avar = array (2,6,7, array (19,18,60));
$nelem = count ($avar, COUNT_RECURSIVE);
echo $nelem;
登入後複製

上面的程式碼將輸出顯示為“7”而不是值“6”。

要對陣列元素進行迭代,我們可以使用for迴圈來迭代。這些值應該會循環繼續執行。因此,在每次迭代步驟中,值都會增加 1。在 count() 方法中使用 for 迴圈時應小心,因為 PHP 缺乏區分索引數組和關聯數組。但大多數程式設計師開發人員假裝使用 count() 而不是 sizeof(),因為它會傳回記憶體大小。儘管它與 count() 函數類似,但大多數人還是堅持使用 count() 函數。

PHP 陣列長度範例

有兩種方法定義 PHP 陣列長度或大小計數。讓我們在下面的範例中看看這些方法是如何決定長度的。

範例#1

建立一個簡單的陣列來計算元素的數量。

代碼:

<?php
$flowers= ['Jasmine', 'Diasy', 'Rose'];
echo "The count is: " . count($flowers);
?>
登入後複製

說明:

  • 當我們執行上述程式碼片段時,輸出顯示為“3”,因為陣列元素有 3 個元素。
  • 首先,我們建立了一個「花」數組,在下一行中,我們使用了 count 指令。

輸出:

PHP 數組長度

範例#2

代碼:

<?php
$program = [
'C++' => ['Polymorphism', 'Inheritance', 'Template'],
'Java' => ['Interface', 'Multithread', 'Exception'],
'PHP' => ['ArrayLength', 'Count']
];
echo "No. of count: ". count($program)."<br>";
echo "Multidimensional count: ". count ($program, 1);
?>
登入後複製

說明:

  • 決定計數為「1」的陣列長度。

輸出:

PHP 數組長度

範例#3

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
$bike=array
(
"Hero Splender"=>array
(
"HP2345",
"HS3456"
),
"Royal Enfield"=>array
(
"R3",
"Tr5"
),
"Honda Activa 6G"=>array
(
"Classic 250"
)
);
echo "General count: " . sizeof($bike)."<br>";
echo "Recursive Number: " . sizeof($bike,1);
?>
</body>
</html>
登入後複製

說明:

  • 上面的程式碼確定了 General 計數為‘3’,遞歸模式的陣列長度為‘8’。

輸出:

PHP 數組長度

範例#4

使用 For 迴圈。

代碼:

<?php
$arr_iter = array (26,60,70,10,130,67);
echo "No of elements in the array = ", sizeof($arr_iter), "<br /><br />";
//Iterating through the array
for ($k=0; $k <sizeof($arr_iter); $k++){
echo "List of elements are: $arr_iter[$k] <br />";
}
?>
登入後複製

說明:

  • The above code takes the length of an array using the sizeof function, and the array elements are iterated using for-loop.
  • The output shows the list of values in an array in each iteration.

Output:

PHP 數組長度

Example #5

Using Null value in mode.

Code:

<?php
$m[0] = 2;
$m[1] = 6;
$m[2] = 8;
value_res(count($m));
$n[3]  = 1;
$n[4]  = 3;
$n[8] = 5;
value_res(count($n));
value_res(count(null));
value_res(count(false));
?>
登入後複製

Explanation:

  • The above code returns a parameter array as the value is assigned as null. So the output looks like this.

Output:

PHP 數組長度

Example #6

Array length Using 2D array.

Code:

<?php
$foods = array('choclates' => array('Diary Milk', 'Cadbury Godiva', 'Nestle','Snikkers',
'Candy Craze'), 'Fast Food' => array('Nuggets', 'Salad Platters'));
echo count($foods, 1);
echo "</br>";
echo sizeof($foods, 1);
?>
登入後複製

Explanation:

  • In the above code, we have used mode count as ‘1’; therefore, this multi-dimensional array counts the value as ‘9’.

Output:

PHP 數組長度

Example #7

Word Count.

Code:

<?Php
$stringtype=' This is EDUCBA Asia largest Web Learning Platform providing courses in various Domains. We Provide Certification from many Leading Universities across the globe.';
$my1_array=explode(" ",$stringtype);
echo "No.Of words in the String = ".sizeof($my1_array);
?>
登入後複製

Explanation:

  • The above program Stores a paragraph in a variable of type String.
  • Here an array is created using explode function to split the array.
  • Finally, count the number of words in a paragraph.
  • We get the count as below.

Output:

PHP 數組長度

Conclusion

Here we have seen how to determine the length or size of an array in PHP and also the various methods to show how PHP functions are used to take memory size used by the array. There is no difference between the count and size of the function. Depends upon the developer, the methods are picked while writing the code. In this article, we explored PHP’s array length with many examples, and also, we have also seen more about multi-dimensional arrays.

以上是PHP 數組長度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!