我有一个条目,该条目可能有多个注释。
如果我在C#中执行此操作,则将构建这些注释的集合。但是,我不熟悉在PHP中执行此操作的方法。
我当前的思考过程是创建一个注释对象,该对象具有一种方法,该方法将基于ID构建和data_array,然后仅调用该对象并以这种方式利用数据。
这是最简单,最基本的方法吗?还是有更好(正确的方式)为PHP构建集合的方法?
我的伪代码:
class entry
var $id
var $text
class comments
var $id
var $entryId
var $text
var $data_array;
function getCollectionById(){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$da[$i] = array('entryId'=>$row["entryId"],'text'=>$text);
}
$this->data_array = $da;
}
最佳答案
数组可以非常有效地模拟集合:
$my_collection = array();
// some loop to populate your collection
$my_collection[] = new Comment(...); // adds a new object to the end of the collection
//end loop
您可以使用foreach遍历它
foreach($my_collection as $comment):
print_r($comment);
endforeach;
关于php - 基本的PHP集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6010172/