Convert PHP object to associative array

2018-12-31 05:31发布

I'm integrating an API to my website which works with data stored in objects while my code is written using arrays.

I'd like a quick and dirty function to convert an object to an array.

标签: php arrays
27条回答
牵手、夕阳
2楼-- · 2018-12-31 06:18

First of all, if you need an array from object you probably should constitutes the data as array first. Think about it.

Don't use foreach statement or JSON transformations. If you're planning this, again you're working with a data structure, not with an object.

If you really need it use object orientated approach to have a clean and maintable code. For example:

Object as array

class PersonArray implements \ArrayAccess, \IteratorAggregate
{
    public function __construct(Person $person) {
        $this->person = $person;
    }
    // ...
 }

If you need all properties use transfer object

class PersonTransferObject
{
    private $person;

    public function __construct(Person $person) {
        $this->person = $person;
    }

    public function toArray() {
        return [
            // 'name' => $this->person->getName();
        ];
    }

 }
查看更多
人间绝色
3楼-- · 2018-12-31 06:18

Short solution of @SpYk3HH

function objectToArray($o)
{
    $a = array();
    foreach ($o as $k => $v)
        $a[$k] = (is_array($v) || is_object($v)) ? objectToArray($v): $v;

    return $a;
}
查看更多
刘海飞了
4楼-- · 2018-12-31 06:18
$Menu = new Admin_Model_DbTable_Menu(); 
$row = $Menu->fetchRow($Menu->select()->where('id = ?', $id));
$Addmenu = new Admin_Form_Addmenu(); 
$Addmenu->populate($row->toArray());
查看更多
余欢
5楼-- · 2018-12-31 06:19

All other answers posted here are only working with public attributes. Here is one solution that works with javabean-like objects using reflection and getters:

function entity2array($entity, $recursionDepth = 2) {
    $result = array();
    $class = new ReflectionClass(get_class($entity));
    foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
        $methodName = $method->name;
        if (strpos($methodName, "get") === 0 && strlen($methodName) > 3) {
            $propertyName = lcfirst(substr($methodName, 3));
            $value = $method->invoke($entity);

            if (is_object($value)) {
                if ($recursionDepth > 0) {
                    $result[$propertyName] = $this->entity2array($value, $recursionDepth - 1);
                } else {
                    $result[$propertyName] = "***";     //stop recursion
                }
            } else {
                $result[$propertyName] = $value;
            }
        }
    }
    return $result;
}
查看更多
笑指拈花
6楼-- · 2018-12-31 06:20

From the first Google hit for "php object to assoc array" we have this:

function object_to_array($data)
{
    if (is_array($data) || is_object($data))
    {
        $result = array();
        foreach ($data as $key => $value)
        {
            $result[$key] = object_to_array($value);
        }
        return $result;
    }
    return $data;
}

Source at codesnippets.joyent.com.

查看更多
余欢
7楼-- · 2018-12-31 06:20

You can also create function in PHP to convert object array.

function object_to_array($object) {
    return (array) $object;
}
查看更多
登录 后发表回答