0

I have following array

object(Illuminate\Database\Eloquent\Collection)#96 (1) {
      ["items":protected]=>
      array(2) {
        [0]=>
        object(Answer)#97 (20) {
          ["connection":protected]=>
          NULL
          ["table":protected]=>
          NULL
          ["primaryKey":protected]=>
          string(2) "id"
          ["perPage":protected]=>
          int(15)
          ["incrementing"]=>
          bool(true)
          ["timestamps"]=>
          bool(true)
          ["attributes":protected]=>
          array(6) {
            ["id"]=>
            int(457256)
            ["question_id"]=>
            int(3124)
            ["user_id"]=>
            int(1235)
            ["answer_text"]=>
            string(44) "Sincere Architects Engineers Private Limited"
            ["created_at"]=>
            string(19) "2015-08-10 06:18:06"
            ["updated_at"]=>
            string(19) "2015-08-10 06:39:33"
          }
          ["original":protected]=>
          array(6) {
            ["id"]=>
            int(457256)
            ["question_id"]=>
            int(3124)
            ["user_id"]=>
            int(1235)
            ["answer_text"]=>
            string(44) "Sincere Architects Engineers Private Limited"
            ["created_at"]=>
            string(19) "2015-08-10 06:18:06"
            ["updated_at"]=>
            string(19) "2015-08-10 06:39:33"
          }
          ["relations":protected]=>
          array(0) {
          }
          ["hidden":protected]=>
          array(0) {
          }
          ["visible":protected]=>
          array(0) {
          }
          ["appends":protected]=>
          array(0) {
          }
          ["fillable":protected]=>
          array(0) {
          }
          ["guarded":protected]=>
          array(1) {
            [0]=>
            string(1) "*"
          }
          ["dates":protected]=>
          array(0) {
          }
          ["touches":protected]=>
          array(0) {
          }
          ["observables":protected]=>
          array(0) {
          }
          ["with":protected]=>
          array(0) {
          }
          ["morphClass":protected]=>
          NULL
          ["exists"]=>
          bool(true)
        }
        [1]=>
        object(Answer)#98 (20) {
          ["connection":protected]=>
          NULL
          ["table":protected]=>
          NULL
          ["primaryKey":protected]=>
          string(2) "id"
          ["perPage":protected]=>
          int(15)
          ["incrementing"]=>
          bool(true)
          ["timestamps"]=>
          bool(true)
          ["attributes":protected]=>
          array(6) {
            ["id"]=>
            int(457257)
            ["question_id"]=>
            int(3125)
            ["user_id"]=>
            int(1235)
            ["answer_text"]=>
            string(14) "Anoop Bartaria"
            ["created_at"]=>
            string(19) "2015-08-10 06:18:06"
            ["updated_at"]=>
            string(19) "2015-08-10 06:39:33"
          }
          ["original":protected]=>
          array(6) {
            ["id"]=>
            int(457257)
            ["question_id"]=>
            int(3125)
            ["user_id"]=>
            int(1235)
            ["answer_text"]=>
            string(14) "Anoop Bartaria"
            ["created_at"]=>
            string(19) "2015-08-10 06:18:06"
            ["updated_at"]=>
            string(19) "2015-08-10 06:39:33"
          }
          ["relations":protected]=>
          array(0) {
          }
          ["hidden":protected]=>
          array(0) {
          }
          ["visible":protected]=>
          array(0) {
          }
          ["appends":protected]=>
          array(0) {
          }
          ["fillable":protected]=>
          array(0) {
          }
          ["guarded":protected]=>
          array(1) {
            [0]=>
            string(1) "*"
          }
          ["dates":protected]=>
          array(0) {
          }
          ["touches":protected]=>
          array(0) {
          }
          ["observables":protected]=>
          array(0) {
          }
          ["with":protected]=>
          array(0) {
          }
          ["morphClass":protected]=>
          NULL
          ["exists"]=>
          bool(true)
        }
      }
    }

It clearly shows that It has two values that is [0] and [1] but when I var_dump(array[0])

it says offset error :0

any leads on this one would be appreciated.

Thanks

1
  • have you tried to var_dump(your-variable-name[0]); it should work Commented Aug 11, 2015 at 5:34

3 Answers 3

2

You can use dd($array->toArray()); to print as Array. Which convert collection in array. Same you can use dd($array->toJson()); to print in Json format.

Hope this help.

Sign up to request clarification or add additional context in comments.

Comments

0

Actually you don't have an array, you have a Colleciton object, you can get items using all (http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_all) method and then you can dump the returned array.

1 Comment

Illuminate\Database\Eloquent\Collection extends Illuminate\Support\Collection which implements ArrayAccess. That means Collection objects can be used like arrays.
0

Please try the following,

var_dump(json_decode($array,true)[0]);

json_decode($array,true) --> this statement will convert the collection object into an array. Once this is done, you can access the array using the array index

Thank you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.