如何将值转换为对象数组?

时间:2012-02-01 14:08:19

标签: php arrays

我有以下值,

firstsession
secondsession
thirdsession
B+

我必须将上面的值转换为对象数组

stdClass Object
(
    [Itemname] => firstsession
    [Ability] => B+
)
stdClass Object
(
    [Itemname] => secondsession
    [Ability] => B+
)
stdClass Object
(
    [Itemname] => thirdsession
    [Ability] => B+
)

怎么做?

3 个答案:

答案 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)

答案 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;
像这样?