tuvi/easycut

一个用于轻松获取简单和多维数组切片的工具

0.0.2 2016-10-13 17:02 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:02:18 UTC


README

关于EasyCut

EasyCut是一个小巧的工具包,通过指定索引、值或数组中存在的键值对,快速在PHP中对简单或多维数组进行切割。

要求

  • php
  • composer

安装

在您的终端/命令行中,运行 composer install tuvi/easycut。确保将 use Tuvi\EasyCut; 添加到您将使用EasyCut的文件顶部。

使用案例

1 EasyCut用于简单数组

使用

<?php

use Tuvi\EasyCut;

$easyCut = new EasyCut(['1, 2, 3, 4, 5, 6, 7, 8, 9, 10']);

以下是一个简单数组的EasyCut示例,以下是可以对EasyCut的$easyCut实例执行和调用的操作和方法:

i). 从原始数组开头获取到索引的子数组,包括该索引处的值

$subArray = $easyCut->arrayToIndex(4); // [1, 2, 3, 4, 5];

ii). 从原始数组开头获取到值的子数组,包括该值

$subArray = $easyCut->arrayToValue(4); // [1, 2, 3, 4];

iii). 从原始数组开头获取到索引之前的子数组,不包括该索引处的值

$subArray = $easyCut->arrayToBeforeIndex(4); // [1, 2, 3, 4];

iv). 从原始数组开头获取到值之前的子数组,不包括该值

$subArray = $easyCut->arrayToValue(4); // [1, 2, 3];

v). 从索引获取到原始数组末尾的子数组,包括该索引处的值

$subArray = $easyCut->arrayFromIndex(4); // [5, 6, 7, 8, 9, 10'];

vi). 从值开始获取到原始数组末尾的子数组,包括该值

$subArray = $easyCut->arrayFromValue(4); // [4, 5, 6, 7, 8, 9, 10'];

vii). 从索引之后获取到原始数组末尾的子数组,不包括该索引处的值

$subArray = $easyCut->arrayAfterIndex(4); // [6, 7, 8, 9, 10'];

viii). 从值之后获取到原始数组末尾的子数组,不包括该值

$subArray = $easyCut->arrayAfterValue(4); // [5, 6, 7, 8, 9, 10'];

2 EasyCut用于多维数组

使用

<?php

use Tuvi\EasyCut;

$easyDeepCut = new EasyCut([
    [
        'index' => 0,
        'position' => 'one',
        'type' => 'animal',
    ],
    [
        'index' => 1,
        'position' => 'two',
        'type' => 'song',
    ],
    [
        'index' => 2,
        'position' => 'three',
        'type' => 'building',
    ],
    [
        'index' => 3,
        'position' => 'four',
        'type' => 'tool',
    ],
    [
        'index' => 4,
        'position' => 'five',
        'type' => 'spice',
    ]
]);

以下是一个多维数组的EasyCut示例,以下是可以对EasyCut的$easyDeepCut实例执行和调用的操作和方法:

ix). 从原始数组开头获取到键值对发生的索引处的子数组,包括键值对发生的索引处的项目

$easyDeepCut->deepArrayTo('building', 'type');
/* 
[
    [
        'index' => 0,
        'position' => 'one',
        'type' => 'animal',
    ],
    [
        'index' => 1,
        'position' => 'two',
        'type' => 'song',
    ],
    [
        'index' => 2,
        'position' => 'three',
        'type' => 'building',
    ]
];
*/

x). 从原始数组开头获取到键值对发生的索引之前的子数组,不包括键值对发生的索引处的项目

$easyDeepCut->deepArrayBefore('building', 'type');
/* 
[
    [
        'index' => 0,
        'position' => 'one',
        'type' => 'animal',
    ],
    [
        'index' => 1,
        'position' => 'two',
        'type' => 'song',
    ]
];
*/

xii). 从多维数组中键值对发生的索引获取到原始数组末尾的子数组,包括键值对发生的索引处的项目

$easyDeepCut->deepArrayFrom('building', 'type');
/*
[
    [
        'index' => 2,
        'position' => 'three',
        'type' => 'building',
    ],
    [
        'index' => 3,
        'position' => 'four',
        'type' => 'tool',
    ],
    [
        'index' => 4,
        'position' => 'five',
        'type' => 'spice',
    ]
]
*/

xiii). 从多维数组中键值对发生的索引之后获取到原始数组末尾的子数组,不包括键值对发生的索引处的项目

$easyDeepCut->deepArrayAfter('building', 'type');
/*
[
    [
        'index' => 3,
        'position' => 'four',
        'type' => 'tool',
    ],
    [
        'index' => 4,
        'position' => 'five',
        'type' => 'spice',
    ]
]
*/