Skip to content

This library allows you to populate an array, pulling properties and nested properties from an object.

License

Notifications You must be signed in to change notification settings

TomHart/array-from-object

Repository files navigation

Array from object

Build Status codecov Scrutinizer Code Quality GitHub release (latest by date) GitHub

This library allows you to populate an array, pulling properties and nested properties from an object.

Usage

Simple example:

$params = ['name', 'id'];

$class = new \stdClass();
$class->name = 'name';
$class->id = 1;

$data = ArrayUtil::populateArrayFromObject($params, $class); 
// ['name' => 'name', 'id' => 1]

Nested example:

$params = ['name', 'id', 'extra->name'];

$class = new \stdClass();
$class->name = 'name';
$class->id = 1;

$class2 = new \stdClass();
$class2->name = 'name-2';
$class->extra = $class2;

$data = ArrayUtil::populateArrayFromObject($params, $class); 
// ['name' => 'name', 'id' => 1, 'extra->name' => 'name-2']

Providing missing data example:

$params = ['name', 'id', 'extra'];

$class = new \stdClass();
$class->name = 'name';
$class->id = 1;

$data = ArrayUtil::populateArrayFromObject($params, $class, [
    'extra' => 'static'
]); 
// ['name' => 'name', 'id' => 1, 'extra' => 'static']

About

This library allows you to populate an array, pulling properties and nested properties from an object.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages