How to convert an array to object in PHP?

2018-12-31 07:56发布

How can i convert an array like this to object?

    [128] => Array
        (
            [status] => Figure A.
 Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
        )

    [129] => Array
        (
            [status] => The other day at work, I had some spare time
        )

)

30条回答
何处买醉
2楼-- · 2018-12-31 08:35

Here are three ways:

  1. Fake a real object:

    class convert
    {
        public $varible;
    
        public function __construct($array)
        {
            $this = $array;
        }
    
        public static function toObject($array)
        {
            $array = new convert($array);
            return $array;
        }
    }
    
  2. Convert the array into an object by casting it to an object:

    $array = array(
        // ...
    );
    $object = (object) $array;
    
  3. Manually convert the array into an object:

    $object = object;
    foreach ($arr as $key => $value) {
        $object->{$key} = $value;
    }
    
查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 08:35

By using (array) and (object) as prefix, you can simply convert object array to standard array and vice-verse

<?php
//defining an array
$a = array('a'=>'1','b'=>'2','c'=>'3','d'=>'4');

//defining an object array
$obj = new stdClass();
$obj->a = '1';
$obj->b = '2';
$obj->c = '3';
$obj->d = '4';

print_r($a);echo '<br>';
print_r($obj);echo '<br>';

//converting object array to array
$b = (array) $obj;
print_r($b);echo '<br>';

//converting array to object
$c = (object) $a;
print_r($c);echo '<br>';
?>
查看更多
刘海飞了
4楼-- · 2018-12-31 08:36

There's no built-in method to do it as far as I'm aware, but it's as easy as a simple loop:

    $obj= new stdClass();

    foreach ($array as $k=> $v) {
        $obj->{$k} = $v;
    }

You can expound on that if you need it to build your object recursively.

查看更多
栀子花@的思念
5楼-- · 2018-12-31 08:37

you can simply use type casting to convert an array to object.

// *convert array to object* Array([id]=> 321313[username]=>shahbaz)
$object = (object) $array_name;

//now it is converted to object and you can access it.
echo $object->username;
查看更多
人间绝色
6楼-- · 2018-12-31 08:38

use this function that i've made:

function buildObject($class,$data){
    $object = new $class;
    foreach($data as $key=>$value){
        if(property_exists($class,$key)){
            $object->{'set'.ucfirst($key)}($value);
        }
    }
    return $object;
}

Usage:

$myObject = buildObject('MyClassName',$myArray);
查看更多
听够珍惜
7楼-- · 2018-12-31 08:39

Little complicated but easy to extend technique:

Suppose you have an array

$a = [
     'name' => 'ankit',
     'age' => '33',
     'dob' => '1984-04-12'
];

Suppose you have have a Person class which may have more or less attributes from this array. for example

class Person 
{
    private $name;
    private $dob;
    private $age;
    private $company;
    private $city;
}

If you still wanna change your array to the person object. You can use ArrayIterator Class.

$arrayIterator = new \ArrayIterator($a); // Pass your array in the argument.

Now you have iterator object.

Create a class extending FilterIterator Class; where you have to define the abstract method accept. Follow the example

class PersonIterator extends \FilterIterator
{
    public function accept()
    {
        return property_exists('Person', parent::current());
    }
}

The above impelmentation will bind the property only if it exists in the class.

Add one more method in the class PersonIterator

public function getObject(Person $object)
{
        foreach ($this as $key => $value)
        {
            $object->{'set' . underscoreToCamelCase($key)}($value);
        }
        return $object;
}

Make sure you have mutators defined in your class. Now you are ready to call these function where you want to create object.

$arrayiterator = new \ArrayIterator($a);
$personIterator = new \PersonIterator($arrayiterator);

$personIterator->getObject(); // this will return your Person Object. 
查看更多
登录 后发表回答