略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SplFixedArray::fromArray

2024-12-21

SplFixedArray::fromArray

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SplFixedArray::fromArrayImport a PHP array in a SplFixedArray instance

说明

public static SplFixedArray::fromArray(array $array, bool $preserveKeys = true): SplFixedArray

Import the PHP array array in a new SplFixedArray instance

参数

array

The array to import.

preserveKeys

Try to save the numeric indexes used in the original array.

返回值

Returns an instance of SplFixedArray containing the array content.

范例

示例 #1 SplFixedArray::fromArray() example

<?php
$fa 
SplFixedArray::fromArray(array(=> 1=> 2=> 3));

var_dump($fa);

$fa SplFixedArray::fromArray(array(=> 1=> 2=> 3), false);

var_dump($fa);
?>

以上例程会输出:

object(SplFixedArray)#1 (4) {
  [0]=>
  int(2)
  [1]=>
  int(1)
  [2]=>
  NULL
  [3]=>
  int(3)
}
object(SplFixedArray)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
add a noteadd a note

User Contributed Notes 1 note

up
5
MuLoT
11 years ago
Memory footprint tests :

<?php
echo memory_get_usage()."\n"; // display 627760
$array = array_fill( 0, 2048, 'a' );
echo
memory_get_usage()."\n"; // 824744, so 196984 for $array

unset( $array );

echo
memory_get_usage()."\n"; // 627792
$spl=SplFixedArray::fromArray( array_fill( 0, 2048, 'a' ) );
echo
memory_get_usage()."\n"; //644944, so just 17151 for $spl !!!
?>

官方地址:https://www.php.net/manual/en/splfixedarray.fromarray.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3