PHP - Ds Vector::toArray() 函式



PHP 的Ds\Vector::toArray() 函式用於將向量轉換為陣列。

此函式不會修改當前向量,並返回一個數組,陣列中的元素順序與向量相同。

語法

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

public Ds\Vector::toArray(): array

引數

此函式不接受任何引數。

返回值

此函式返回一個包含所有值的陣列,其順序與向量相同。

示例 1

以下程式演示了 PHP Ds\Vector::toArray() 函式的使用:

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo("The original vector: \n"); 
   print_r($vector);
   echo("An array elements: \n");
   print_r($vector->toArray());
?>

輸出

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

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
An array elements:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

示例 2

以下是 PHP Ds\Vector::toArray() 函式的另一個示例。我們使用此函式將向量轉換為陣列 (["Tutorials", "Point", "Tutorix"]):

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo("The original vector: \n"); 
   print_r($vector);
   echo("An array elements: \n"); 
   print_r($vector->toArray()); 
?>

輸出

上述程式產生以下輸出:

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
An array elements:
Array
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
php_function_reference.htm
廣告