Spl标准库 - SplArray

优质
小牛编辑
132浏览
2023-12-01

SplArray其实是对ArrayObject的实现。

  1. $spl = new CoreComponentSplSplArray(array(
  2. "a"=>1,
  3. "b"=>array(
  4. "sub"=>'b1',
  5. "sub2"=>"b2"
  6. ),
  7. "c"=>array(
  8. "sub"=>'c1',
  9. "sub2"=>"c2"
  10. )
  11. ));

get

  1. var_dump($spl->get("a"));
  2. int(1)
  3. var_dump($spl->get("c.sub"));
  4. string(2) "c1"
  5. var_dump($spl->get("*.sub"));
  6. array(2) {
  7. ["b"]=>
  8. string(2) "b1"
  9. ["c"]=>
  10. string(2) "c1"
  11. }

set

  1. $spl->set("a",2);
  2. var_dump($spl->get("a"));
  3. $spl->set("c.sub",2);
  4. var_dump($spl->get("c.sub"));

getArrayCopy

  1. var_dump($spl->getArrayCopy());
  2. array(3) {
  3. ["a"]=>
  4. int(1)
  5. ["b"]=>
  6. array(2) {
  7. ["sub"]=>
  8. string(2) "b1"
  9. ["sub2"]=>
  10. string(2) "b2"
  11. }
  12. ["c"]=>
  13. array(2) {
  14. ["sub"]=>
  15. string(2) "c1"
  16. ["sub2"]=>
  17. string(2) "c2"
  18. }
  19. }

__toString

  1. echo $spl;
  2. {"a":1,"b":{"sub":"b1","sub2":"b2"},"c":{"sub":"c1","sub2":"c2"}}

注意:SplArray中对toString方法的实习实际上是json_encode()