0

我有一个未知数量的对象,这些对象应该被分类到带有动态游戏的循环内的另一个对象中,所以我可以稍后在我的脚本中调用它,如下所示:

$object->$variable5->(attribut from my other object)

这是我目前拥有的。

$object= new stdClass;
$i=0;
while ($row = mysql_fetch_object($result)){
    $object->variable.$i = $row;
    $i++;
}

我尝试安静了一下并进行了搜索,但没有找到真正的解决方案...

4

1 回答 1

1
$attribute = "$variable$i";
$object->$attribute = $row;

但这只是为了回答您有关如何操作的问题。据我所知,您最好使用数组作为成员变量:

$object = (object)array($variable => array());
$i=0;
while ($row = mysql_fetch_object($result)){
    $object->{$variable}[] = $row;
    $i++;
}

然后您可以$object->$variable5->(attribut from my other object)访问

$object->{$variable}[5]->(attribut from my other object)
于 2013-10-11T16:38:43.263 回答