2017 © Pedro Peláez
 

library array-utils

Useful functions for working with arrays

image

shadiakiki1986/array-utils

Useful functions for working with arrays

  • Friday, January 6, 2017
  • by shadiakiki1986
  • Repository
  • 1 Watchers
  • 0 Stars
  • 345 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 10 Versions
  • 3 % Grown

The README.md

ArrayUtils

ArrayUtils is a collection of useful PHP array functions., (*1)

Build Status Packagist, (*2)

This is a fork of theodorejb/array-utils: Packagist Version License Build Status, (*3)

Major changes are that I require php>=7 for the sake of the parameter types in CellCounterManager functions, (*4)

Install via Composer

composer require shadiakiki1986/array-utils, (*5)

and add bootstrap, (*6)

require_once __DIR__."/vendor/autoload.php";, (*7)

Functions

contains_all

Returns true if all the needles are in the haystack., (*8)

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

contains_same

Returns true if both arrays contain the same values (regardless of order)., (*9)

use function theodorejb\ArrayUtils\contains_same;

$set1 = [1, 3, 5, 7];
$set2 = [3, 7, 5, 1];

echo contains_same($set1, $set2); // true

group_rows

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., (*10)

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)

array3d2xlsx

Dumps array of arrays as xlsx file, with each subarray as a sheet, (*11)

Requires apt-get install php-zip and composer require PHPOffice/PHPExcel, (*12)

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., (*13)

$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., (*14)

Memory issues: * for large excel files, phpexcel could run out of memory (check here) * Need apt-get install sqlite3 * pass true for the isLarge (2nd) parameter to array3d2xlsx, (*15)

array2console

Dumps a 3d array to a string in tabular format for viewing in the console, (*16)

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

array2html

Dumps a 3d array to a html string in tabular format for viewing in a browser, (*17)

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

Author

Theodore Brown
http://theodorejb.me, (*18)

Shadi Akiki, (*19)

License

MIT, (*20)

Dev notes

If phpcs reports errors that can be fixed automatically, run vendor/bin/phpcbf src/ and then commit the changes, (*21)

The Versions

06/01 2017

dev-master

9999999-dev

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

04/01 2017

2.0.5

2.0.5.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

30/12 2016

2.0.4

2.0.4.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

04/11 2016

2.0.3

2.0.3.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

08/10 2016

2.0.2

2.0.2.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

07/10 2016

2.0.1

2.0.1.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

07/10 2016

2.0.0

2.0.0.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

 

The Development Requires

by Theodore Brown
by Shadi Akiki

24/05 2016

v1.1.0

1.1.0.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown

14/01 2016

v1.0.1

1.0.1.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown

14/01 2016

v1.0.0

1.0.0.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown