在 PHP 中将数组转换为用户定义的对象
Convert Array to User Defined Object in PHP
我有一个数组如下 -
$xml= array(
'name'=> 'Arun',
'roll' => '12345',
'id' => '1'
);
我希望将其转换为用户定义对象[不是 stdClass 也不是 SimpleXMLObject]
我想要像下面这样的东西 -
UserDefinedObject Object
(
[name] => Arun
[roll] => 12345
[id] => 1
)
我做了类似 belo 的事情,它给了我 stdClass 对象,但我需要它是 UserDefined -
public static function convert($xml)
{
//$array = new ConvertArrayToXML($xml);
//return $array;
//return((Object)$xml);
foreach($xml as $key => $value)
{
if(is_array($value))
{
$array[$key] = self::convert($value);
}
}
return (object)$xml;
}
我知道这听起来很傻,但我对在 php 中转换用户定义的对象还很陌生。
请指点一下。
谢谢。
$xml = [
'name'=> 'Arun',
'roll' => '12345',
'id' => '1',
];
class UserDefinedObject { }
$myObj = new UserDefinedObject();
foreach($xml as $key => $value) {
$myObj->{$key} = $value;
}
var_dump($myObj);
编辑
或略有不同:
$myObj = new UserDefinedObject();
array_walk($xml,
function($value, $key) use ($myObj) {
$myObj->{$key} = $value;
}
);
class UserDefinedClass {
public $name;
public $roll;
public $id;
function __construct($name,$roll,$id){
$this->name = $name;
$this->roll = $roll;
$this->id = $id;
}
}
$xml= array(
'name'=> 'Arun',
'roll' => '12345',
'id' => '1'
);
$obj = new UserDefinedClass($xml['name'],$xml['roll'],$xml['id']);
// you can use and foreach for dynamic elements.
print_r($obj);
$xml = array('name' => 'Arun', 'roll' => '12345', 'id' => '1');
class 用户自定义 {
public $name;
public $roll;
public $id;
function __construct($xml) {
$this->name = $xml['name'];
$this->roll = $xml['roll'];
$this->id = $xml['id'];
}
}
print_r(新用户定义($xml));
我有一个数组如下 -
$xml= array(
'name'=> 'Arun',
'roll' => '12345',
'id' => '1'
);
我希望将其转换为用户定义对象[不是 stdClass 也不是 SimpleXMLObject]
我想要像下面这样的东西 -
UserDefinedObject Object
(
[name] => Arun
[roll] => 12345
[id] => 1
)
我做了类似 belo 的事情,它给了我 stdClass 对象,但我需要它是 UserDefined -
public static function convert($xml)
{
//$array = new ConvertArrayToXML($xml);
//return $array;
//return((Object)$xml);
foreach($xml as $key => $value)
{
if(is_array($value))
{
$array[$key] = self::convert($value);
}
}
return (object)$xml;
}
我知道这听起来很傻,但我对在 php 中转换用户定义的对象还很陌生。
请指点一下。
谢谢。
$xml = [
'name'=> 'Arun',
'roll' => '12345',
'id' => '1',
];
class UserDefinedObject { }
$myObj = new UserDefinedObject();
foreach($xml as $key => $value) {
$myObj->{$key} = $value;
}
var_dump($myObj);
编辑
或略有不同:
$myObj = new UserDefinedObject();
array_walk($xml,
function($value, $key) use ($myObj) {
$myObj->{$key} = $value;
}
);
class UserDefinedClass {
public $name;
public $roll;
public $id;
function __construct($name,$roll,$id){
$this->name = $name;
$this->roll = $roll;
$this->id = $id;
}
}
$xml= array(
'name'=> 'Arun',
'roll' => '12345',
'id' => '1'
);
$obj = new UserDefinedClass($xml['name'],$xml['roll'],$xml['id']);
// you can use and foreach for dynamic elements.
print_r($obj);
$xml = array('name' => 'Arun', 'roll' => '12345', 'id' => '1');
class 用户自定义 {
public $name;
public $roll;
public $id;
function __construct($xml) {
$this->name = $xml['name'];
$this->roll = $xml['roll'];
$this->id = $xml['id'];
}
}
print_r(新用户定义($xml));