PHP - Ds Set::toArray() 函式



PHP 的 Ds\Set::toArray() 函式用於將物件或集合(例如集合)轉換為陣列。

此函式不會修改當前集合,並返回轉換後的陣列,該陣列包含集合的值,而不改變值的順序。

語法

以下是 PHP Ds\Set::toArray() 函式的語法:

public array Ds\Set::toArray( void )

引數

此函式不接受任何引數。

返回值

此函式返回一個數組,其中包含所有值,順序與集合相同。

示例 1

以下是 PHP Ds\Set::toArray() 函式的基本示例。

<?php  
   $set = new \Ds\Set([1, 2, 3, 4, 5]); 
   echo "The set elements are: \n";
   print_r($set);
   echo "The array is: \n"; 
   print_r($set->toArray()); 
?> 

輸出

執行上述程式後,將顯示以下輸出:

The set elements are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The array is:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

示例 2

以下是 PHP Ds\Set::toArray() 函式的另一個示例。我們使用此函式建立一個數組,其中包含所有值,順序與該集合 (["Tutorials", "Point", "India"]) 相同。

<?php  
   $set = new \Ds\Set(["Tutorials", "Point", "India"]); 
   echo "The set elements are: \n";
   print_r($set);
   echo "The array is: \n"; 
   $arr = $set->toArray();
   print_r($arr);
?>

輸出

執行上述程式後,將顯示以下輸出:

The set elements are:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The array is:
Array
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
php_function_reference.htm
廣告