PHP Ds Stack toArray()函数用法介绍

2021年3月13日17:18:15 发表评论 885 次浏览

Ds \ Stack :: toArray()PHP函数用于将堆栈转换为数组并返回转换后的数组。此功能不会修改实际的堆栈。

语法如下:

void public Ds\Stack::toArray ()

参数:该函数不接受任何参数。

返回值:此函数返回从堆栈生成的数组。

下面的程序说明了Ds \ Stack :: toArray()功能:

程序1:

<?php
  
// PHP program to illustarte the 
// toArray() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
  
// Print the converted array
print_r( $stack ->toArray());
  
// Print the Stack
print_r( $stack );
  
?>

输出如下:

Array
(
    [0] => GfG
    [1] => to
    [2] => Welcome
)
Ds\Stack Object
(
    [0] => GfG
    [1] => to
    [2] => Welcome
)

程式2:

<?php
  
// PHP program to illustarte the 
// toArray() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing Mixed value elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
$stack ->push(10);
$stack ->push(5.5);
  
// Print the converted Array
print_r( $stack ->toArray());
  
// Print the Stack
print_r( $stack );
  
?>

输出如下:

Array
(
    [0] => 5.5
    [1] => 10
    [2] => GfG
    [3] => to
    [4] => Welcome
)
Ds\Stack Object
(
    [0] => 5.5
    [1] => 10
    [2] => GfG
    [3] => to
    [4] => Welcome
)

参考:http://php.net/manual/en/ds-stack.toarray.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: