helpers\BaseArrayHelper toArray()

toArray() public static method

Converts an object or an array of objects into an array.

public static array toArray ( $object, $properties = [], $recursive = true )
$object object|array|string

The object to be converted into an array

$properties array

A mapping from object class names to the properties that need to put into the resulting arrays. The properties specified for each class is an array of the following format:

[
    'app\models\Post' => [
        'id',
        'title',
        // the key name in array result => property name
        'createTime' => 'created_at',
        // the key name in array result => anonymous function
        'length' => function ($post) {
            return strlen($post->content);
        },
    ],
]

The result of ArrayHelper::toArray($post, $properties) could be like the following:

[
    'id' => 123,
    'title' => 'test',
    'createTime' => '2013-01-01 12:00AM',
    'length' => 301,
]
$recursive boolean

Whether to recursively converts properties which are objects into arrays.

return array

The array representation of the object

doc_Yii
2016-10-30 17:04:29
Comments
Leave a Comment

Please login to continue.