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

$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 - PHP对象数组::无法将<MyObject>类型的对象用作数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27197171/

10-09 17:26