Search code examples
phparrayscodeignitercodeigniter-3arrayobject

Unable to access data from array inside foreach loop - Codeigniter


My DB has 2 rows of values : Test2 campaign & premium dove

view page

foreach ($estedit as $estrow) { 
                 //print_r($estrow);
                 echo $estrow->name;
                              }

The above code shows these 2 values at the bottom as in the below linked image, but it displays error Trying to get property 'name' of non-object.

Error Screenshot:

https://i.sstatic.net/g7wtz.png

print_r($estrow); inside the foreach loop shows the below given data:

stdClass Object ( [est_id] => 3 [name] => Test2 campaign ) stdClass Object ( [est_id] => 1 [name] => premium dove ).

Accessing the name field as $estrow->name shows the error Illegal string offset 'name'. What am i doing wrong? How can i access the name field here ?

Controller

 $estedit[] = '';

                    foreach ($est_id as $item) {

                        $estedit[] = $this->Report->get_estedit($item);

                        $estlineedit[] = $this->Report->get_estline_edit($item);
                    }
                    $data['estedit'] = $estedit;
                    $data['estlineedit'] = $estlineedit;
                    $data['date'] = $date;
                    $this->load->view('reports/download_report_view', $data);

Solution

  • You set empty string as first element of $estedit

     $estedit[] = '';
    

    Looks like you want

     $estedit = [];
    

    instead