php - 基本的PHP集合

标签 php

我有一个条目,该条目可能有多个注释。

如果我在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/

相关文章:

php - 表的有序列表

php - 查询未执行

php - 创建广告多维数组并使用 foreach 循环显示它

php - 优化 WordPress Woocommerce 导入脚本

php - Laravel Eloquent ORM 混淆

php - Paypal 捐赠按钮?

php - 基于PHP/MySQL生成x个html元素

php - 如何从 php/mysql 的下拉列表中选择所有选项

php - 标题已经通过冲洗功能发送了吗?

php - 'is_array' 和 '\is_array' 之间的区别