简体   繁体   中英

Fatal error:Cannot use object of type stdClass as array in php

I am sending data from controller to mode,

I got below result, I was trying to display the data in grid view.

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
                    )

            )

    )

)

But I got error like this.

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];

This is my PHP code.

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>';
        }
    }

}    

what i need to do..

This error means you're trying to access an object field as it was an array. That will never work.

for accessing object fields

$object->field_name

for accessing array fields

$array[field_name]

Maybe you'll find useful cast your object to array

$array_var = (array) $object_var;

You are doing it the wrong way. Say, $result has the entire stdClass object , then the following code segment should print the elements correctly.

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

Your question is not clear, but as per your need you can try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM