致命错误:不能在php中使用stdClass类型的对象作为数组

时间:2014-02-18 10:55:55

标签: php

我正在从控制器向模式发送数据,

我得到了以下结果,我试图在网格视图中显示数据。

stdClass Object
(
[Tmchb] => stdClass Object
    (
        [item] => Array
            (
                [0] => stdClass Object
                    (
                        [Matnr] => 1006-1
                        [J3asize] => S
                        [Maktx] => STOCKHOLM-BLANC
                        [Clabs] => 42.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

                [1] => stdClass Object
                    (
                        [Matnr] => 1006-10
                        [J3asize] => XXL
                        [Maktx] => STOCKHOLM-ROUGE
                        [Clabs] => 85.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

                [2] => stdClass Object
                    (
                        [Matnr] => 1006-10
                        [J3asize] => XL
                        [Maktx] => STOCKHOLM-ROUGE
                        [Clabs] => 66.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

                [3] => stdClass Object
                    (
                        [Matnr] => 1006-10
                        [J3asize] => S
                        [Maktx] => STOCKHOLM-ROUGE
                        [Clabs] => 58.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

                [4] => stdClass Object
                    (
                        [Matnr] => 1006-10
                        [J3asize] => M
                        [Maktx] => STOCKHOLM-ROUGE
                        [Clabs] => 167.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

                [5] => stdClass Object
                    (
                        [Matnr] => 1006-10
                        [J3asize] => L
                        [Maktx] => STOCKHOLM-ROUGE
                        [Clabs] => 230.0
                        [Incweek] => 
                        [Zincmgstk] => 0.0
                    )

            )

    )

但是我得到了这样的错误。

Fatal error: Cannot use object of type stdClass as array in `C:\xampp\htdocs\portal\components\com_portal\views\portal\tmpl\default_stock_details_list.php` on line 139

$grid->CellData =$gridData[0];

这是我的PHP代码。

function JTPlatinumGrid1RowData($sender, $params) {
$rows = &$params[ 1 ];
$rowdata=$rows;
$alignArray1=array();
    foreach($rowdata as $key=>$val){
        $alignArray1[]=$key;
    }

        for($i=1; $i<=count($alignArray1); $i++){

        if($rows[$alignArray1[$i]]!= ''){
        $rows[$alignArray1[$i]] = '<div style="text-align:right;">' . $rows[$alignArray1[$i]]  . '  </div>';
        }
    }

}    

我需要做什么..

3 个答案:

答案 0 :(得分:0)

此错误表示您尝试访问对象字段,因为它是一个数组。那永远不会奏效。

用于访问对象字段

$object->field_name

用于访问数组字段

$array[field_name]

也许你会发现将对象转换为数组

$array_var = (array) $object_var;

答案 1 :(得分:0)

你这样做是错误的。 比如说, $ result 包含整个 stdClass对象,那么以下代码段应该正确打印元素。

foreach ($result->Tmchb->item as $row) {
  print $row->Matnr . " " . $row->J3asize;
}

答案 2 :(得分:0)

您的问题不明确,但根据您的需要,您可以试试这个

foreach($grid->CellData->$gridData as row){
  echo row['your value'];
}