divineomega/object_to_array

此PHP包提供了一个 `object_to_array` 辅助函数。

v1.0.0 2018-02-12 15:07 UTC

This package is auto-updated.

Last update: 2024-09-24 09:32:15 UTC


README

Build Status Coverage Status StyleCI

此PHP包提供了一个 object_to_array 辅助函数。您可以使用此函数将对象转换为数组。

安装

您可以使用Composer依赖管理器轻松安装 `object_to_array` 包。只需从项目的根目录运行以下命令。

composer require "divineomega/object_to_array"

如果您以前从未使用过Composer依赖管理器,请访问Composer网站 获取更多信息。

使用方法

以下是几个使用 object_to_array 辅助函数的示例。

// Basic object

$object = new stdClass;
$object->name = 'John';
$object->age = 32;

$array = object_to_array($object)

/*
$array = [
    'name' => 'John',
    'age' => 32
];
*/
// Object with nested object

$object = new stdClass;
$object->name = 'John';
$object->age = 32;
$object->pet = new stdClass;
$object->pet->type = 'cat';
$object->pet->name = 'Mr Fluffkins The Third';

$array = object_to_array($object)

/*
$array = [
    'name' => 'John',
    'age' => 32,
    'pet' => [
        'name' => 'Mr Fluffkins The Third',
        'type' => 'cat'
    ]
];
*/
// Object with nested array

$object = new stdClass;
$object->name = 'John';
$object->age = 32;
$object->favouriteFoods = [
    'pizza',
    'cake'
];

$array = object_to_array($object)

/*
$array = [
    'name' => 'John',
    'age' => 32,
    'favouriteFoods' => [
        'pizza',
        'cake'
    ]
];
*/