PHP code example of tomhart / array-from-object

1. Go to this page and download the library: Download tomhart/array-from-object library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomhart / array-from-object example snippets


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

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

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

$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']

$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']