我有以下值,
firstsession
secondsession
thirdsession
B+
我必须将上面的值转换为对象数组
stdClass Object
(
[Itemname] => firstsession
[Ability] => B+
)
stdClass Object
(
[Itemname] => secondsession
[Ability] => B+
)
stdClass Object
(
[Itemname] => thirdsession
[Ability] => B+
)
怎么做?
答案 0 :(得分:1)
您可以创建一个小班级
<?php
class Item {
public $Itemname;
public $Ability;
public function __construct($name,$ability) {
$this->Itemname = $name;
$this->Ability = $ability;
}
}
$arr = array(
new Item('firstsession','B+'),
new Item('secondsession','B+'),
new Item('thirdsession','B+'),
);
print_r($arr);
?>
答案 1 :(得分:0)
也许你正在寻找... http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/
快速搜索; - )
答案 2 :(得分:0)
$arr = array();
$a = new stdClass;
$a->Itemname = "first session";
$a->Ability = "B+";
$b = new stdClass;
$b->Itemname = "second session";
$b->Ability = "B+";
$c = new stdClass;
$c->Itemname = "third session";
$c->Ability = "B+";
$arr[] = $a;
$arr[] = $b;
$arr[] = $c;
像这样?