mulertech/array-manipulation
Composer 安装命令:
composer require mulertech/array-manipulation
包简介
This class manipulates and organizes arrays
README 文档
README
This class manipulates and organizes arrays
Installation
Two methods to install Application package with composer :
Add to your "composer.json" file into require section :
"mulertech/array-manipulation": "^2.0"
and run the command :
php composer.phar update
Run the command :
php composer.phar require mulertech/array-manipulation "^2.0"
Usage
Check if an array is associative or not :
ArrayManipulation::isAssoc([['name' => 'john'], ['name' => 'jack']]);
// true
Check if an array is list or not :
ArrayManipulation::isList([1, 2, 3, 4, 5]);
// true
List of duplicates values :
ArrayManipulation::listOfDuplicates([1, 2, 3, 33, 8, 33, 4, 806, 402, 806]);
// [33, 806]
Add a "number" key (or another name) and its number into an associative array :
ArrayManipulation::addNumberKey([['name' => 'john'], ['name' => 'jack']]);
// [['name' => 'john', 'number' => 1], ['name' => 'jack', 'number' => 2]]
Find differences by name (it can be used to compare old and new array) :
$first = ['akey' => 'avalue', 'anotherkey' => 'secondvalue', 'thirdkey' => 'oldvalue'];
$second = ['akey' => 'avalue', 'anotherkey' => 'notsamevalue', 'thirdkey' => 'newvalue'];
ArrayManipulation::findDifferencesByName($first, $second);
// ['anotherkey' => ['secondvalue', 'notsamevalue'], 'thirdkey' => ['oldvalue', 'newvalue']];
Order by one or two column (you can choose the name of the columns and the asc or desc order for each) :
$array = [
['firstcolumn' => 'apple', 'secondcolumn' => 'zorro', 'thirdcolumn' => 'masked man'],
['firstcolumn' => 'banana', 'secondcolumn' => 'superman', 'thirdcolumn' => 'masked man'],
['firstcolumn' => 'cherry', 'secondcolumn' => 'batman', 'thirdcolumn' => 'masked man'],
['firstcolumn' => 'coconut', 'secondcolumn' => 'alibaba', 'thirdcolumn' => 'unmasked man'],
['firstcolumn' => 'pineapple', 'secondcolumn' => 'Tom sawyer', 'thirdcolumn' => 'unmasked man']
];
ArrayManipulation::orderByColumn($array, 'thirdcolumn', 'desc', 'secondcolumn'); // default order is asc
// result :
[
['firstcolumn' => 'coconut', 'secondcolumn' => 'alibaba', 'thirdcolumn' => 'unmasked man'],<br>
['firstcolumn' => 'pineapple', 'secondcolumn' => 'Tom sawyer', 'thirdcolumn' => 'unmasked man'],<br>
['firstcolumn' => 'cherry', 'secondcolumn' => 'batman', 'thirdcolumn' => 'masked man'],<br>
['firstcolumn' => 'banana', 'secondcolumn' => 'superman', 'thirdcolumn' => 'masked man'],<br>
['firstcolumn' => 'apple', 'secondcolumn' => 'zorro', 'thirdcolumn' => 'masked man'],<br>
];
Add a value into an array by indicated the key, the value and keys up to final key,
it can create the final key or add the value into an array in this final key :
- Add a key and its value :
$array = [
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b'
]
]
];
ArrayManipulation::addKeyValue($array, 'keytoaddvalue', ['a first value'], 'subtest3', 'othersub');
// result :
[
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b',
'keytoaddvalue' => ['a first value']
]
]
];
- Add a value to an existing key :
$array = [
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b',
'keytoaddvalue' => ['a first value']
]
]
];
ArrayManipulation::addKeyValue($array, 'keytoaddvalue', 'an other value', 'subtest3', 'othersub');
// result :
[
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b',
'keytoaddvalue' => ['a first value', 'an other value']
]
]
];
Remove a key and its value into an array by indicated the key and keys up to final key :
$array = [
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b',
'keytoremove' => 'goodbye'
]
]
];
ArrayManipulation::removeKey($array, 'subtest3', 'othersub', 'keytoremove');
// Result :
[
'subtest1' => [
'subsubtest1' => 'a value',
'subsubtest2' => 'b value',
'subsubtest3' => 'c value'
],
'subtest2' => [
'subsubsecondtest1' => 'another value a',
'subsubsecondtest2' => 'another value b',
'subsubsecondtest3' => 'another value c',
'subsubsecondtest4' => 'another value d',
],
'subtest3' => [
'othersub' => [
'subsubsub1' => 'value a',
'subsubsub2' => 'value b',
]
]
];
mulertech/array-manipulation 适用场景与选型建议
mulertech/array-manipulation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 497 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mulertech/array-manipulation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mulertech/array-manipulation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 497
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-27