如何将这样的数组转换为对象?
[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"
)
如何将这样的数组转换为对象?
[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"
)
当前回答
通过使用(array)和(object)作为前缀,可以简单地将对象数组转换为标准数组,反之亦然
<?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>';
?>
其他回答
有点复杂,但很容易扩展的技术:
假设你有一个数组
$a = [
'name' => 'ankit',
'age' => '33',
'dob' => '1984-04-12'
];
假设您有一个Person类,它可能有来自这个数组的或多或少的属性。例如
class Person
{
private $name;
private $dob;
private $age;
private $company;
private $city;
}
如果你还想把数组改成person对象。你可以使用ArrayIterator类。
$arrayIterator = new \ArrayIterator($a); // Pass your array in the argument.
现在你有了迭代器对象。
创建一个扩展FilterIterator class的类;你必须定义抽象方法accept。遵循示例
class PersonIterator extends \FilterIterator
{
public function accept()
{
return property_exists('Person', parent::current());
}
}
上面的实现只在类中存在该属性时才会绑定它。
在类PersonIterator中再添加一个方法
public function getObject(Person $object)
{
foreach ($this as $key => $value)
{
$object->{'set' . underscoreToCamelCase($key)}($value);
}
return $object;
}
确保在类中定义了mutator。 现在,您可以在想要创建对象的地方调用这些函数。
$arrayiterator = new \ArrayIterator($a);
$personIterator = new \PersonIterator($arrayiterator);
$personIterator->getObject(); // this will return your Person Object.
我使用的一个(它是类成员):
const MAX_LEVEL = 5; // change it as needed
public function arrayToObject($a, $level=0)
{
if(!is_array($a)) {
throw new InvalidArgumentException(sprintf('Type %s cannot be cast, array expected', gettype($a)));
}
if($level > self::MAX_LEVEL) {
throw new OverflowException(sprintf('%s stack overflow: %d exceeds max recursion level', __METHOD__, $level));
}
$o = new stdClass();
foreach($a as $key => $value) {
if(is_array($value)) { // convert value recursively
$value = $this->arrayToObject($value, $level+1);
}
$o->{$key} = $value;
}
return $o;
}
据我所知,没有内置的方法可以做到这一点,但它就像一个简单的循环一样简单:
$obj= new stdClass();
foreach ($array as $k=> $v) {
$obj->{$k} = $v;
}
如果你需要递归地构建你的对象,你可以详细说明。
如果你需要将一个数组强制转换为一个特定的类(在我的例子中,我需要对象类型为Google_Service_AndroidPublisher_Resource_Inappproducts),你可以像这样从stdClass中将类名str_replace为预期的类:
function castArrayToClass(array $array, string $className)
{
//first cast the array to stdClass
$subject = serialize((object)$array);
//then change the class name
$converted = str_replace(
'O:8:"stdClass"',
'O:'.strlen($className).':"'.$className.'"',
$subject
);
unset($subject);
return unserialize($converted);
}
这里有三种方法:
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; } } Convert the array into an object by casting it to an object: $array = array( // ... ); $object = (object) $array; Manually convert the array into an object: $object = object; foreach ($arr as $key => $value) { $object->{$key} = $value; }