Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/divineomega/object_to_array
This PHP package provides an `object_to_array` helper function. You can use this function to convert an object to an array.
https://github.com/divineomega/object_to_array
array helper-functions object php php-library
Last synced: 24 days ago
JSON representation
This PHP package provides an `object_to_array` helper function. You can use this function to convert an object to an array.
- Host: GitHub
- URL: https://github.com/divineomega/object_to_array
- Owner: DivineOmega
- License: lgpl-3.0
- Created: 2018-02-12T15:01:17.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-23T23:43:30.000Z (over 4 years ago)
- Last Synced: 2024-10-15T02:38:59.592Z (about 1 month ago)
- Topics: array, helper-functions, object, php, php-library
- Language: PHP
- Size: 8.79 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `object_to_array` helper function
[![Build Status](https://travis-ci.org/DivineOmega/object_to_array.svg?branch=master)](https://travis-ci.org/DivineOmega/object_to_array)
[![Coverage Status](https://coveralls.io/repos/github/DivineOmega/object_to_array/badge.svg?branch=master)](https://coveralls.io/github/DivineOmega/object_to_array?branch=master)
[![StyleCI](https://styleci.io/repos/121260505/shield?branch=master)](https://styleci.io/repos/121260505)This PHP package provides an `object_to_array` helper function. You can use this function to convert an object to an array.
## Installation
The `object_to_array` package can be easily installed using Composer. Just run the following command from the root of your project.
```
composer require "divineomega/object_to_array"
```If you have never used the Composer dependency manager before, head to the [Composer website](https://getcomposer.org/) for more information on how to get started.
## Usage
Here are a few examples of how to use the `object_to_array` helper function.
```php
// Basic object$object = new stdClass;
$object->name = 'John';
$object->age = 32;$array = object_to_array($object)
/*
$array = [
'name' => 'John',
'age' => 32
];
*/
``````php
// 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'
]
];
*/
``````php
// 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'
]
];
*/
```