SplArray

用途

用于处理数组数据,比如排序,过滤等等。

核心对象方法

核心类:EasySwoole\Spl\SplArray。

get

获取数组中某项的值。

  • mixed $name 数据项索引

function get($path)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->get('fruit.apple'));
  23. /**
  24. * 输出结果过:
  25. * int(2)
  26. */

set

设置数组中某项的值。

  • mixed $path 数据项索引
  • mixed $value 数据

function set($path, $value)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. $splArray->set('fruit.apple', 3);
  23. var_dump($splArray->get('fruit.apple'));
  24. /**
  25. * 输出结果过:
  26. * int(3)
  27. */

__toString

进行 JSON 编码

function __toString()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->__toString());
  23. /**
  24. * 输出结果过:
  25. * string(80) "{"fruit":{"apple":2,"orange":1,"grape":4},"color":{"red":12,"blue":8,"green":6}}"
  26. */

getArrayCopy

强制转换成数组

function getArrayCopy()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->__toString());
  23. /**
  24. * 输出结果过:
  25. * array(2) {
  26. * ["fruit"]=>
  27. * array(3) {
  28. * ["apple"]=>
  29. * int(2)
  30. * ["orange"]=>
  31. * int(1)
  32. * ["grape"]=>
  33. * int(4)
  34. * }
  35. * ["color"]=>
  36. * array(3) {
  37. * ["red"]=>
  38. * int(12)
  39. * ["blue"]=>
  40. * int(8)
  41. * ["green"]=>
  42. * int(6)
  43. * }
  44. * }
  45. *
  46. */

unset

去除某个数据项

  • mixed $path 数据项索引

function unset($path)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. $splArray->unset('fruit.apple');
  23. var_dump($splArray->__toString());
  24. /**
  25. * 输出结果过:
  26. * array(2) {
  27. * ["fruit"]=>
  28. * array(2) {
  29. * ["orange"]=>
  30. * int(1)
  31. * ["grape"]=>
  32. * int(4)
  33. * }
  34. * ["color"]=>
  35. * array(3) {
  36. * ["red"]=>
  37. * int(12)
  38. * ["blue"]=>
  39. * int(8)
  40. * ["green"]=>
  41. * int(6)
  42. * }
  43. }
  44. */

delete

去除某个数据项

  • mixed $key 数据项索引

public function delete($key)

ps: unset和delete方法其实是实现统一效果,因考虑旧版本用户使用情况,故而保留。

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 12,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. $splArray->delete('fruit.apple');
  23. var_dump($splArray->__toString());
  24. /**
  25. * 输出结果过:
  26. * array(2) {
  27. * ["fruit"]=>
  28. * array(2) {
  29. * ["orange"]=>
  30. * int(1)
  31. * ["grape"]=>
  32. * int(4)
  33. * }
  34. * ["color"]=>
  35. * array(3) {
  36. * ["red"]=>
  37. * int(12)
  38. * ["blue"]=>
  39. * int(8)
  40. * ["green"]=>
  41. * int(6)
  42. * }
  43. * }
  44. */

unique

移除数组中重复的值

public function unique()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->unique()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(4) {
  21. * ["apple"]=>
  22. * int(2)
  23. * ["orange"]=>
  24. * int(1)
  25. * ["pear"]=>
  26. * int(4)
  27. * ["banana"]=>
  28. * int(8)
  29. * }
  30. */

multiple

获取数组中重复的值

public function multiple()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->unique()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(1) {
  21. * ["grape"]=>
  22. * int(2)
  23. * }
  24. */

asort

进行排序并保持索引关系

public function asort()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->asort()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(5) {
  21. * ["orange"]=>
  22. * int(1)
  23. * ["apple"]=>
  24. * int(2)
  25. * ["grape"]=>
  26. * int(2)
  27. * ["pear"]=>
  28. * int(4)
  29. * ["banana"]=>
  30. * int(8)
  31. * }
  32. */

ksort

按照键名排序

public function ksort()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->ksort()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(5) {
  21. * ["apple"]=>
  22. * int(2)
  23. * ["banana"]=>
  24. * int(8)
  25. * ["grape"]=>
  26. * int(2)
  27. * ["orange"]=>
  28. * int(1)
  29. * ["pear"]=>
  30. * int(4)
  31. * }
  32. */

sort

自定义排序

  • mixed $sort_flags 比较类型

public function sort($sort_flags = SORT_REGULAR)

  • SORT_REGULAR - 正常比较单元(不改变类型)
  • SORT_NUMERIC - 单元被作为数字来比较
  • SORT_STRING - 单元被作为字符串来比较
  • SORT_LOCALE_STRING - 根据当前的区域(locale)设置来把单元当作字符串比较,可以用 setlocale() 来改变
  • SORT_NATURAL - 和 natsort() 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。
  • SORT_FLAG_CASE - 能够与 SORT_STRING 或 SORT_NATURAL 合并(OR 位运算),不区分大小写排序字符串。

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->ksort()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(5) {
  21. * [0]=>
  22. * int(1)
  23. * [1]=>
  24. * int(2)
  25. * [2]=>
  26. * int(2)
  27. * [3]=>
  28. * int(4)
  29. * [4]=>
  30. * int(8)
  31. * }
  32. */

column

取得某一列

  • mixed $column 列索引
  • mixed $index_key 返回数组的索引/键的列

public function column($column, $index_key = null)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->column('apple')->getArrayCopy());
  23. /**
  24. * 输出结果过:
  25. * array(1) {
  26. * [0]=>
  27. * int(2)
  28. * }
  29. */

flip

交换数组中的键和值

public function flip()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->flip()->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(4) {
  21. * [2]=>
  22. * string(5) "grape"
  23. * [1]=>
  24. * string(6) "orange"
  25. * [4]=>
  26. * string(4) "pear"
  27. * [8]=>
  28. * string(6) "banana"
  29. * }
  30. */

filter

过滤数组数据

  • mixed $keys 需要取得/排除的键
  • mixed $exclude true则排除设置的键名 false则仅获取设置的键名

public function filter($keys, $exclude = false)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 2,
  13. 'pear' => 4,
  14. 'banana' => 8
  15. ];
  16. $splArray = new \EasySwoole\Spl\SplArray($data);
  17. var_dump($splArray->filter('apple,orange')->getArrayCopy());
  18. /**
  19. * 输出结果过:
  20. * array(2) {
  21. * ["apple"]=>
  22. * int(2)
  23. * ["orange"]=>
  24. * int(1)
  25. * }
  26. */

keys

获取数组索引

  • mixed $path 数据项索引

public function keys($path = null)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->keys('fruit'));
  23. /**
  24. * 输出结果过:
  25. * array(3) {
  26. * [0]=>
  27. * string(5) "apple"
  28. * [1]=>
  29. * string(6) "orange"
  30. * [2]=>
  31. * string(5) "grape"
  32. * }
  33. */

values

获取数组中所有的值

public function values()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->values()->getArrayCopy());
  23. /**
  24. * 输出结果过:
  25. * array(2) {
  26. * [0]=>
  27. * array(3) {
  28. * ["apple"]=>
  29. * int(2)
  30. * ["orange"]=>
  31. * int(1)
  32. * ["grape"]=>
  33. * int(4)
  34. * }
  35. * [1]=>
  36. * array(3) {
  37. * ["red"]=>
  38. * int(2)
  39. * ["blue"]=>
  40. * int(8)
  41. * ["green"]=>
  42. * int(6)
  43. * }
  44. * }
  45. */

flush

清空数据

public function flush()

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->flush()->getArrayCopy());
  23. /**
  24. * 输出结果过:
  25. * array(0) {
  26. * }
  27. */

loadArray

重新加载数据

  • array $data 数据

public function loadArray(array $data)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. $data = [
  23. 'apple' => 2,
  24. 'orange' => 1,
  25. 'grape' => 2,
  26. 'pear' => 4,
  27. 'banana' => 8
  28. ];
  29. var_dump($splArray->loadArray($data)->getArrayCopy());
  30. /**
  31. * 输出结果过:
  32. * array(5) {
  33. * ["apple"]=>
  34. * int(2)
  35. * ["orange"]=>
  36. * int(1)
  37. * ["grape"]=>
  38. * int(2)
  39. * ["pear"]=>
  40. * int(4)
  41. * ["banana"]=>
  42. * int(8)
  43. * }
  44. */

toXML

转化成xml

  • mixed $CD_DATA 是否需要CDTA标志
  • mixed $rootName 跟节点名称
  • mixed $encoding 编码

public function toXML($CD_DATA = false,$rootName = ‘xml’,$encoding = ‘UTF-8’)

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 上午10:10
  7. */
  8. require './vendor/autoload.php';
  9. $data = [
  10. 'fruit' => [
  11. 'apple' => 2,
  12. 'orange' => 1,
  13. 'grape' => 4
  14. ],
  15. 'color' => [
  16. 'red' => 2,
  17. 'blue' => 8,
  18. 'green' => 6
  19. ]
  20. ];
  21. $splArray = new \EasySwoole\Spl\SplArray($data);
  22. var_dump($splArray->toXML());
  23. /**
  24. * 输出结果过:
  25. * string(134) "<xml><fruit><apple>2</apple><orange>1</orange><grape>4</grape></fruit><color><red>2</red><blue>8</blue><green>6</green></color></xml>"
  26. */