ArrayUtils is a collection of useful PHP array functions.
This is a fork of theodorejb/array-utils:
Major changes are that I require php>=7
for the sake of the parameter types in CellCounterManager
functions
composer require shadiakiki1986/array-utils
and add bootstrap
require_once __DIR__."/vendor/autoload.php";
Returns true if all the needles are in the haystack.
use function theodorejb\ArrayUtils\contains_all;
$haystack = [1, 2, 3, 5, 8, 13];
$needles = [2, 13, 5];
echo contains_all($needles, $haystack); // true
echo contains_all($haystack, $needles); // false
Returns true if both arrays contain the same values (regardless of order).
use function theodorejb\ArrayUtils\contains_same;
$set1 = [1, 3, 5, 7];
$set2 = [3, 7, 5, 1];
echo contains_same($set1, $set2); // true
Splits the array of associative arrays into groups when the specified key value changes. The array must be sorted by the array key used to group results.
use function theodorejb\ArrayUtils\group_rows;
// obtained by joining tables of people and their pets
$peoplePets = [
['name' => 'Jack', 'petName' => 'Scruffy'],
['name' => 'Jack', 'petName' => 'Spot'],
['name' => 'Jack', 'petName' => 'Paws'],
['name' => 'Amy', 'petName' => 'Blackie'],
['name' => 'Amy', 'petName' => 'Whiskers']
];
$grouped = [];
foreach (group_rows($peoplePets, 'name') as $group) {
$grouped[] = $group;
}
$expected = [
[
$peoplePets[0],
$peoplePets[1],
$peoplePets[2],
],
[
$peoplePets[3],
$peoplePets[4],
]
];
var_dump($grouped === $expected); // bool(true)
Dumps array of arrays as xlsx file, with each subarray as a sheet
Requires apt-get install php-zip
and composer require PHPOffice/PHPExcel
use shadiakiki1986\ArrayUtils\Converters;
// obtained by joining tables of people and their pets
$peoplePets = [
['name' => 'Jack', 'petName' => 'Scruffy'],
['name' => 'Jack', 'petName' => 'Spot'],
['name' => 'Jack', 'petName' => 'Paws'],
['name' => 'Amy', 'petName' => 'Blackie'],
['name' => 'Amy', 'petName' => 'Whiskers']
];
var_dump(Converters::array3d2xlsx(array("people-pets"=>$peoplePets))); // returns path to xlsx filename in temporary directory
For excel dates in the cells, use the \DateTime class for the php values, e.g.
$people = [
['name' => 'Roula', 'dateOfBirth' => \DateTime::createFromFormat('!Y-m-d','1982-10-05')],
['name' => 'Shadi', 'dateOfBirth' => \DateTime::createFromFormat('!Y-m-d','1986-09-22')]
];
Note the !
preceding Y-m-d
above resets the hours/minutes/seconds to 0 so that they don't show up in the excel data autofilter.
Check the docs for DateTime::createFromFormat for more details.
Memory issues:
- for large excel files, phpexcel could run out of memory (check here)
- Need
apt-get install sqlite3
- pass
true
for theisLarge
(2nd) parameter toarray3d2xlsx
Dumps a 3d array to a string in tabular format for viewing in the console
use shadiakiki1986\ArrayUtils\Converters;
// obtained by joining tables of people and their pets
$peoplePets = [
['name' => 'Jack', 'petName' => 'Scruffy'],
['name' => 'Jack', 'petName' => 'Spot'],
['name' => 'Jack', 'petName' => 'Paws'],
['name' => 'Amy', 'petName' => 'Blackie'],
['name' => 'Amy', 'petName' => 'Whiskers']
];
echo(Converters::array2console($peoplePets)); // outputs array in tabular format
Dumps a 3d array to a html string in tabular format for viewing in a browser
use shadiakiki1986\ArrayUtils\Converters;
// obtained by joining tables of people and their pets
$peoplePets = [
['name' => 'Jack', 'petName' => 'Scruffy'],
['name' => 'Jack', 'petName' => 'Spot'],
['name' => 'Jack', 'petName' => 'Paws'],
['name' => 'Amy', 'petName' => 'Blackie'],
['name' => 'Amy', 'petName' => 'Whiskers']
];
echo(Converters::array2html($peoplePets)); // outputs array in html table
Theodore Brown
http://theodorejb.me
Shadi Akiki
MIT
If phpcs
reports errors that can be fixed automatically, run vendor/bin/phpcbf src/
and then commit the changes