3

I am new to this and failed to understand why this is happening. I am getting result from database they are:

ArrayArray ( [0] => stdClass Object ( [id] => 1 [box] => draggable1 [xorder] => 1 [descp] => Y ) [1] => stdClass Object ( [id] => 2 [box] => draggable2 [xorder] => 2 [descp] => Z ) [2] => stdClass Object ( [id] => 3 [box] => draggable3 [xorder] => 3 [descp] => X ) ) 

I am trying to print single value such as printing only id value 1 or box value draggable1 from list of array. But problem is when i apply

echo $result1['data'];

gives error:

Severity: Notice
Message Array to String conversion 

similarly while further trying by doing:

echo $result1['data']['id']; //id is field name

gives me error:

undefined index : id

and lastly when i got tired did this:

print_r($result1['data'][0]);

and printed the result

stdClass Object ( [id] => 1 [box] => draggable1 [xorder] => 1 [descp] => Y ) 

but when i do so:

print_r($result1['data'][0]['id']);

gives error:

Fatal error: Cannot use object of type stdClass as array

i follow this post which is related to me but i dont know why i am unable to do so Codeigniter : displaying query result in controller

Here is my function:

public function index()
    {
        $id=$this->input->post('name');
        $result1['data']=$this->drag_model->getcoordinates();
        $result2['data']=$this->drag_model->getva($id);

        echo $result1['data']['id'];
        print_r($result1['data'][0]['id']);
    //  array('id'=>);
        //  $this->load->view('drag/drag_view');
        $this->load->view('drag/dragswap',$result1);

    //  $this->load->view('LoginN/login_view');

    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
giantmalik
  • 137
  • 1
  • 7
  • Rizier123 i ve already mentioned the reason and at the below i ve mentioned my closest post but i went fail – giantmalik Dec 29 '16 at 07:59

1 Answers1

1

Your last error message got you where you need to be, but you're trying to use an object as an array. Objects are referenced like this:

$result1['data'][0]->id
aynber
  • 22,380
  • 8
  • 50
  • 63