Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.
/ Util Public archive

[DEPRECATED] A collection of utility classes for everyday use. lodash-php instead

License

Notifications You must be signed in to change notification settings

SolidWorx/Util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Util

Build Status

DEPRECATED This package is not maintained anymore. Use solidworx/lodash-php instead

A collection of utility classes for everyday use

Table of Contents

Requirements

Minimum PHP requirement is PHP 7.1+

Installation

Composer

$ composer require solidworx/util

Usage

ArrayUtil

column:

Return the values from a single column in the input array. The input can be an array of arrays or objects. This method is an enhancement to the normal array_column function. This can be useful if you need to get a specific value from a collection.

Using an array input

<?php
$input = [
    ['test' => 'one'],
    ['test' => 'two'],
    ['test' => 'three'],
];

$columns = ArrayUtil::column($input, 'test');

/* $columns = array (
                  0 => 'one',
                  1 => 'two',
                  2 => 'three',
              );*/

Using an object with public properties

<?php

class Foo {
    public $test;
}

$foo1 = new Foo;
$foo1->test = 'one';

$foo2 = new Foo;
$foo2->test = 'two';

$foo3 = new Foo;
$foo3->test = 'three';

$input = [
    $foo1,
    $foo2,
    $foo3,
];

$columns = ArrayUtil::column($input, 'test');

/* $columns = array (
                  0 => 'one',
                  1 => 'two',
                  2 => 'three',
              );*/

Using an object with methods

<?php

class Foo {
    private $value;
    
    public function __construct($value)
    {
        $this->value = $value;
    }
    
    public function test()
    {
        return $this->>value;
    }
}

$input = [
    new Foo('one'),
    new Foo('two'),
    new Foo('three'),
];

$columns = ArrayUtil::column($input, 'test');

/* $columns = array (
                  0 => 'one',
                  1 => 'two',
                  2 => 'three',
              );*/

Using an object with getters

<?php

class Foo {
    private $value;
    
    public function __construct($value)
    {
        $this->value = $value;
    }
    
    public function getTest()
    {
        return $this->value;
    }
}

$input = [
    new Foo('one'),
    new Foo('two'),
    new Foo('three'),
];

$columns = ArrayUtil::column($input, 'test');

/* $columns = array (
                  0 => 'one',
                  1 => 'two',
                  2 => 'three',
              );*/

Getting all the email addresses of your users:

<?php

$users = $userRepository->findAll();

$emails = ArrayUtil::column($users, 'email');

By default, all the null values are filtered out. If you want to keep the null values, pass false as the third parameter:

$users = $userRepository->findAll();

$emails = ArrayUtil::column($users, 'email', false); // Will keep empty values in the result 

Testing

To run the unit tests, execute the following command

$ vendor/bin/phpunit

Contributing

See CONTRIBUTING

License

This library is open-sourced software licensed under the MIT license

Please see the LICENSE file for the full license.

About

[DEPRECATED] A collection of utility classes for everyday use. lodash-php instead

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages