mulertech/array-manipulation
最新稳定版本:v2.0.2
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',
]
]
];
统计信息
- 总下载量: 471
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-27