SplArray使用

SplArray 支持链式操作,如:$array->unique()->asort()->keys();

命名空间地址:

  1. use \EasySwoole\Core\Component\Spl\SplArray;

获得默认数组格式:

  1. function getArrayCopy(): array

设置数组中元素:

  1. function set($path, $value): void

如:$config->set(‘database.host’,’127.0.0.1’);

获得值:

  1. function get($path)
  1. $splArray = new SplArray([
  2. 'config' => [
  3. 'mysql' => [
  4. 'name' => 'xxxx',
  5. 'host' => 'xxxx',
  6. ],
  7. 'php' => [
  8. 'name' => 'xxxx',
  9. 'host' => 'xxxx',
  10. ]
  11. ]
  12. ]);
  13. var_dump($splArray->get('config.mysql.'));
  14. /*
  15. [
  16. 'name' => 'xxxx',
  17. 'host' => 'xxxx',
  18. ]
  19. */

删除元素:

  1. function delete($key): void
  1. $splArray = new SplArray([
  2. 'config' => [
  3. 'mysql' => [
  4. 'name' => 'xxxx',
  5. 'host' => 'xxxx',
  6. ],
  7. 'php' => [
  8. 'name' => 'xxxx',
  9. 'host' => 'xxxx',
  10. ]
  11. ],
  12. 'other' => ['i m other']
  13. ]);
  14. /*
  15. [ 'other' => ['i m other'] ]
  16. */

数组去重取唯一的值:

  1. function unique(): SplArray

获取数组中重复的值:

  1. function multiple(): SplArray

按照键值升序:

  1. function asort(): SplArray

按照键升序:

  1. function ksort(): SplArray

自定义排序:

  1. function sort($sort_flags = SORT_REGULAR): SplArray

取得某一列:

  1. function column($column, $index_key = null): SplArray

交换数组中的键和值:

  1. function flip(): SplArray

过滤本数组:

  1. function filter($keys, $exclude = false): SplArray

提取数组中的键:

  1. function keys(): SplArray

提取数组中的值:

  1. function values(): SplArray

清空:

  1. function flush():SplArray