SplArray

用于处理数组封装的基础工具

相关class位置

  • SplArray
    • namespace: EasySwoole\Spl\SplArray

SplArray相关方法

方法列表

方法名称参数说明备注
set$key,$value设置参数
get$key获取参数
__toString转字符
getArrayCopy数组赋值
unset$key销毁数组元素
delete$key去除某个数据项
unique数组值唯一
multiple获取数组中重复的值
asort进行排序并保持索引关系
ksort按照键名排序
sort$sort_flags = SORT_REGULAR自定义排序
column$key取得某一列
flip交换数组中的键和值
filter$key,[$key2….],$bool过滤数组数据
keys$key获取数组索引
values获取数组中所有的值
flush清空数据
loadArray$data重新加载数据
toXML转化成xml

基本使用

  1. /**
  2. *
  3. * User: LuffyQAQ
  4. * Date: 2019/10/16 16:02
  5. * Email: <1769360227@qq.com>
  6. */
  7. include "./vendor/autoload.php";
  8. $data = [
  9. 'fruit' => [
  10. 'apple' => 2,
  11. 'orange' => 1,
  12. 'grape' => 4
  13. ],
  14. 'color' => [
  15. 'red' => 12,
  16. 'blue' => 8,
  17. 'green' => 6
  18. ],
  19. 'name' => [
  20. 'name1' => '北溟有鱼QAQ',
  21. 'name2' => '北溟有鱼QAQ'
  22. ]
  23. ];
  24. $splArray = new \EasySwoole\Spl\SplArray($data);
  25. $splArray->set('fruit.apple', 3);
  26. var_dump($splArray);
  27. var_dump($splArray->get('fruit.apple'));
  28. var_dump($splArray->__toString());
  29. var_dump($splArray->getArrayCopy());
  30. var_dump($splArray->unset('color'));
  31. var_dump($splArray->delete('fruit.apple'));
  32. var_dump($splArray->unique()->getArrayCopy());
  33. var_dump($splArray->multiple()->getArrayCopy());
  34. var_dump($splArray->asort()->getArrayCopy());
  35. var_dump($splArray->ksort()->getArrayCopy());
  36. var_dump($splArray->sort()->getArrayCopy());
  37. var_dump($splArray->column('name')->getArrayCopy());
  38. $splArrays = new \EasySwoole\Spl\SplArray(['es' => 'easyswoole']);
  39. var_dump($splArrays->flip()->getArrayCopy());
  40. var_dump($splArray->filter('green,grape', false)->getArrayCopy());
  41. var_dump($splArray->filter('green,grape', true)->getArrayCopy());
  42. var_dump($splArray->keys('name'));
  43. var_dump($splArray->values()->getArrayCopy());
  44. var_dump($splArray->flush()->getArrayCopy());
  45. var_dump($splArray->loadArray(['name' => 'easyswoole'])->getArrayCopy());
  46. var_dump($splArray->toXML());