[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"
)
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:
[127] => stdClass Object
(
[status] => Have you ever created a really great looking website design
)
[128] => stdClass Object
(
[status] => Figure A.
Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
)
[129] => stdClass Object
(
[status] => The other day at work, I had some spare time
)
<?php
class Person {
private $name;
private $age;
private $sexe;
function __construct ($payload)
{
if (is_array($payload))
$this->from_array($payload);
}
public function from_array($array)
{
foreach(get_object_vars($this) as $attrName => $attrValue)
$this->{$attrName} = $array[$attrName];
}
public function say_hi ()
{
print "hi my name is {$this->name}";
}
}
print_r($_POST);
$mike = new Person($_POST);
$mike->say_hi();
?>
// *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;
/**
* Makes a config object from an array, making the first level keys properties a new object.
* Property values are converted to camelCase and are not set if one already exists.
* @param array $configArray Config array.
* @param boolean $strict To return an empty object if $configArray is not an array
* @return stdObject The config object
*/
public function makeConfigFromArray($configArray = [],$strict = true)
{
$object = new stdClass();
if (!is_array($configArray)) {
if(!$strict && !is_null($configArray)) {
return $configArray;
}
return $object;
}
foreach ($configArray as $name => $value) {
$_name = camel_case($name);
if(is_array($value)) {
$makeobject = true;
foreach($value as $key => $val) {
if(is_numeric(substr($key,0,1))) {
$makeobject = false;
}
if(is_array($val)) {
$value[$key] = $this->makeConfigFromArray($val,false);
}
}
if($makeobject) {
$object->{$name} = $object->{$_name} = $this->makeConfigFromArray($value,false);
}
else {
$object->{$name} = $object->{$_name} = $value;
}
}
else {
$object->{$name} = $object->{$_name} = $value;
}
}
return $object;
}
这将把yaml配置为
fields:
abc:
type: formfield
something:
- a
- b
- c
- d:
foo:
bar
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。
现在你已经准备好调用这些你想要创建object的函数
$arrayiterator = new \ArrayIterator($a);
$personIterator = new \PersonIterator($arrayiterator);
$personIterator->getObject(); // this will return your Person Object.
$arr= [128=> ['status'=>
'Figure A. Facebook \'s horizontal scrollbars showing up on a 1024x768 screen resolution.'],
129=>['status'=>'The other day at work, I had some spare time']];
$ArrToObject=(object)$arr;
var_dump($ArrToObject);
结果将是一个包含数组的对象:
对象(stdClass)#1048(2){[128]=>数组(1){
< p >["地位"]= >
字符串(87)“图a: Facebook的水平滚动条显示在1024x768分辨率的屏幕上。”} < / p >
[129]=> array(1) {
["地位"]= >
字符串(44)"The other day at work, I have some spare time"}}