略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Collection

2024-11-15

The Collection interface

(No version information available, might only be in Git)

简介

Collection is the base interface which covers functionality common to all the data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode().

接口摘要

class Ds\Collection implements Countable, IteratorAggregate, JsonSerializable {
/* 方法 */
abstract public clear(): void
abstract public copy(): Ds\Collection
abstract public isEmpty(): bool
abstract public toArray(): array
}

更新日志

版本 说明
PECL ds 1.4.0 Collection implements IteratorAggregate now instead of just Traversable. (This change came to the polyfill in 1.4.1.)

目录

add a noteadd a note

User Contributed Notes 1 note

up
-11
depakin at yisangwu dot com
2 years ago
// Collection
   $collection_a = new \Ds\Vector([1, 2, 3]);
   $collection_b = new \Ds\Vector();

   var_dump($collection_a, $collection_b);
    /*
        object(Ds\Vector)[1]
        public 0 => int 1
        public 1 => int 2
        public 2 => int 3

        object(Ds\Vector)[2]
    */

   //json_encode
   var_dump( json_encode($collection_a));
    /*
    string '[1,2,3]
    */
  
    //count
   var_dump(count($collection_a));
    /*
    int 3
    */
  
    // serialize
    var_dump(serialize($collection_a));
    /*
    string 'C:9:"Ds\Vector":12:{i:1;i:2;i:3;}'
    */

    // foreach
    foreach ($collection_a as $key => $value) {
       echo $key ,'--', $value, PHP_EOL;
    }
    /*
       0--1
       1--2
       2--3
     */

    // clone
    $clone = clone($collection_a);
    var_dump($clone);
    /*
      object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

    // push
    $clone->push('aa');
    var_dump($clone);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => string 'aa' (length=2)
     */

   // isEmpty
   var_dump($collection_a->isEmpty(), $collection_b->isEmpty());
    /*
    boolean false
    boolean true
     */

   // toArray
   var_dump($collection_a->toArray(), $collection_b->toArray());
    /*
     array (size=3)
      0 => int 1
      1 => int 2
      2 => int 3

    array (size=0)
      empty
    */

   // copy ( void )
   //浅拷贝, shallow copy
   $collection_c = $collection_a->copy();

   var_dump($collection_c);
    /*
    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
    */

   $collection_c->push(4);
   var_dump($collection_a, $collection_c);
    /*
    object(Ds\Vector)[1]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3

    object(Ds\Vector)[3]
      public 0 => int 1
      public 1 => int 2
      public 2 => int 3
      public 3 => int 4
    */
  
    // clear
    $collection_a->clear();
    $collection_b->clear();
    $collection_c->clear();

    var_dump($collection_a, $collection_b, $collection_c);
    /*
    object(Ds\Vector)[1]
    object(Ds\Vector)[2]
    object(Ds\Vector)[3]
    */

官方地址:https://www.php.net/manual/en/class.ds-collection.php

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