wake/php-array-kit

简单函数帮助在PHP中使用数组

v0.4.2 2019-07-20 15:01 UTC

This package is auto-updated.

Last update: 2024-09-21 02:15:05 UTC


README

简单函数帮助在PHP中使用数组。

用法

keyi ($array, $key, $gather = false)

基于键分割数组

  $input = [
    ['id' => 'A', 'title' => 'A apple', 'value' => 10],
    ['id' => 'B', 'title' => 'B bear',  'value' => 27],
    ['id' => 'C', 'title' => 'C Cat',   'value' => 10],
  ];

  // Split array base on key

  $output = keyi ($input, 'id');

  /*
  [
    'A' => ['id' => 'A', 'title' => 'A apple', 'value' => 10],
    'B' => ['id' => 'B', 'title' => 'B bear',  'value' => 27],
    'C' => ['id' => 'C', 'title' => 'C Cat',   'value' => 10],
  ];
  */

  // Base on multiple keys

  $output = keyi ($input, ['id', 'title']);

  /*
  [
    'A' => [
      'A apple' => ['id' => 'A', 'title' => 'A apple', 'value' => 10]
    ],
    'B' => [
      'B bear'  => ['id' => 'B', 'title' => 'B bear',  'value' => 27]
    ],
    'C' => [
      'C Cat'   => ['id' => 'C', 'title' => 'C Cat',   'value' => 10]
    ],
  ];
  */

  // Gather elements

  $output = keyi ($input, 'value', true);

  /*
  [
    '10' => [
      ['id' => 'A', 'title' => 'A apple', 'value' => 10],
      ['id' => 'B', 'title' => 'B bear',  'value' => 27],
    ],
    '27' => [
      ['id' => 'C', 'title' => 'C Cat',   'value' => 10],
    ]
  ];
  */

dig ($array, $key = false, $deep = 1)

从数组中挖掘值

  $input = [
    'A' => [
      'id' => 'A',
      'A apple' => [
        'value' => 10
      ]
    ],
    'B' => [
      'id' => 'B',
      'B bear' => [
        'value' => 27
      ]
    ],
    'C' => [
      'id' => 'C',
      'C Cat' => [
        'value' => 10
      ]
    ],
  ];

  // Dig value from array

  $output = dig ($input, 'id');

  /*
  [
    0 => 'A',
    1 => 'B',
    2 => 'C',
  ];
  */

  // Dig deeply

  $output = dig ($input, 'value', 2);

  /*
  [
    0 => 10,
    1 => 27,
    2 => 10,
  ];
  */

otoa ($object)

对象转数组,从对象值获取数组。

安装

在您的composer.json中添加以下require条目

{
  "require": {
    "wake/php-array-kit": "*"
  }
}

或使用composer

$ composer require wake/php-array-kit:*

然后运行composer installcomposer update

反馈

请随时提出问题并告诉我您的想法或疑问 😃

许可证

MIT许可证下发布