PHP 对象数组::不能使用 <MyObject> 类型的对象作为数组

标签 php mysql arrays

我有一个类代表数据库中的一个表 我想将表填充到对象数组中,如下所示:

$subCat = array();
$count=0;
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $subCatName = $line["sub_cat_name"];
    $subCatShortDescription = $line["short_description"];
    $subCatLongDescription = $line["long_description"];
    $subCat = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);
    $subCat[$count++] = $subCat;
}

我收到以下错误:

Fatal error: Cannot use object of type SubCat as array in C:\AppServ\www\MyWebSite\classes\SubCat.php on line 34

谢谢

最佳答案

您将对象用作数组:

$subCat = array():
// ... code
$subCat = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);
$subCat[$count++] = $subCat;

当您将新对象分配给 $subCat 时,它不再是数组,因此 $subCat[$index]

而是使用类似的东西:

$subCat = array();
// ... code
$subCat[$count++] = new SubCat($countryId, $catName, $subCatShortDescription, $subCatLongDescription);                  

关于PHP 对象数组::不能使用 <MyObject> 类型的对象作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197171/

相关文章:

mysql - "Could not load file or assembly ' MySql.Web“但是我没有使用MySQL?

javascript - 如何使用angularJS过滤选中的复选框

c++ - 访问动态二维字符数组 C++

php - ajax post 调用返回未定义的成功消息

php - 将每一行用作包含的获取变量?

php - 有没有办法测试 php 代码是否兼容 php < 5.3.2

mysql - 将数据从 Azure SQL 表链接并导出到我的 SQL 表

python - 使用Python以MySQL json列格式插入Json数据

java - Java中的equals vs Arrays.equals

php - 使用 SQL 计算记录并在 PHP 中返回