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

    }
1
  • Rizier123 i ve already mentioned the reason and at the below i ve mentioned my closest post but i went fail Commented Dec 29, 2016 at 7:59

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks man. This tired me alot. (y)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.