cryonighter/object-column

This package provides the object_column() function, which works similarly to the array_column() function of the standard library, but having the ability to work with objects

2.0.0 2025-09-06 14:28 UTC

This package is not auto-updated.

Last update: 2025-09-06 14:35:41 UTC


README

Latest Version on Packagist Software License Total Downloads

This package provides the object_column() function, which works similarly to the array_column() function of the standard library, but having the ability to work with objects.

To search for columns in objects, both the public properties of these objects and the methods getColumnName() / hasColumnName() / isColumnName() / columnName() (in that exact order), as well as by the methods of the ArrayAccess interface.

Function object_column() is fully backward compatible with array_column() and can work with regular arrays the same way.

In addition, the object_column() function supports call chainings.

Highlights

  • Simple API
  • Framework Agnostic
  • Composer ready, PSR-2 and PSR-4 compliant

System Requirements

You need:

  • PHP >= 8.1.0 but the latest stable version of PHP is recommended

Install

Via Composer

$ composer require cryonighter/object-column

Usage

The function works as follows:

Example №1: simple access to public properties
use function Cryonighter\ObjectColumn\object_column;

$objects = [
    new class {
        public $foo = '123';
        public $bar = '456';
        public $baz = '789';
    },
    new class {
        public $foo = 'qwe';
        public $bar = 'asd';
        public $baz = 'zxc';
    },
];

$result = object_column($objects, 'foo', 'bar');
Example №2: chain of calls to getters
use function Cryonighter\ObjectColumn\object_column;

$objects = [
    new class {
        public function getFoo(): object {
            return new class {
                public function baz(): string {
                    return '123';
                }
            };
        }
        public function getBar(): object {
            return new class {
                public function buz(): string {
                    return '456';
                }
            };
        }
    },
    new class {
        public function getFoo(): object {
            return new class {
                public function baz(): string {
                    return 'qwe';
                }
            };
        }
        public function getBar(): object {
            return new class {
                public function buz(): string {
                    return 'asd';
                }
            };
        }
    },
];

$result = object_column($objects, 'foo.baz', 'bar.buz');
Example №3: chain of calls to ArrayAccess objects
use function Cryonighter\ObjectColumn\object_column;

$objects = [
    new ArrayObject([
        'foo' => new ArrayObject(['baz' => '123']),
        'bar' => new ArrayObject(['buz' => '456']),
    ]),
    new ArrayObject([
        'foo' => new ArrayObject(['baz' => 'qwe']),
        'bar' => new ArrayObject(['buz' => 'asd']),
    ]),
];

$result = object_column($objects, 'foo.baz', 'bar.buz');
Result

In all cases, the result will be the same

[456 => '123', 'asd' => 'qwe']

Callable arguments

For more complex cases, you can pass your own handlers, which will be applied to all objects of the first nesting level. You will have to implement the handle of the following nesting levels yourself in your callback functions.

Example №4: callable arguments
use function Cryonighter\ObjectColumn\object_column;

$objects = [
    new class {
        public function getFoo(): string {
            return '123';
        }
        public function getBar(): string {
            return '456';
        }
        public function getBaz(): string {
            return '789';
        }
    },
    new class {
        public function getFoo(): string {
            return 'qwe';
        }
        public function getBar(): string {
            return 'asd';
        }
        public function getBaz(): string {
            return 'zxc';
        }
    },
];

$result = object_column(
    $objects,
    fn(object $object): string => $object->getFoo() . '-' . $object->getBar(),
    fn(object $object): string => $object->getBar() . '-' . $object->getBaz(),
);
Result
[
    '456-789' => '123-456',
    'asd-zxc' => 'qwe-asd',
];

Array indexing

Also, the function can be used to index an array, for this it is enough not to pass the first argument

Example №5: array indexing
use function Cryonighter\ObjectColumn\object_column;

$objects = [
    [
        'foo' => ['baz' => '123'],
        'bar' => ['buz' => '456'],
    ],
    [
        'foo' => ['baz' => 'qwe'],
        'bar' => ['buz' => 'asd'],
    ],
];

$result = object_column($objects, null, 'bar.buz');
// or
$result = object_column($objects, indexKey: 'bar.buz');
Result
[
    '456' => [
        'foo' => ['baz' => '123'],
        'bar' => ['buz' => '456'],
    ],
    'asd' => [
        'foo' => ['baz' => 'qwe'],
        'bar' => ['buz' => 'asd'],
    ],
];

For clarity, an example with a simple array is given, but it will work for all the cases listed above.

If no second or third argument is passed to the function, the original array will be returned. This operation is meaningless.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ php vendor/phpunit/phpunit/phpunit tests

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email cryonighter@yandex.ru instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.