This class manipulates and organizes arrays
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"
ArrayManipulation::isAssoc([['name' => 'john'], ['name' => 'jack']]);
// true
ArrayManipulation::isList([1, 2, 3, 4, 5]);
// true
ArrayManipulation::listOfDuplicates([1, 2, 3, 33, 8, 33, 4, 806, 402, 806]);
// [33, 806]
ArrayManipulation::addNumberKey([['name' => 'john'], ['name' => 'jack']]);
// [['name' => 'john', 'number' => 1], ['name' => 'jack', 'number' => 2]]
$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 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']
]
]
];
$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',
]
]
];