1

I have a query:

$stockBook = DB::select(DB::Raw(" my query"));
dump($stockBook);

My o/p:

array:1 [▼
  0 => {#533 ▼
    +"BOOKID": "1"
    +"REMAINING": 17.0
  }
]

I want to retrieve the REMAINING attribute data, but I am getting error. I tried :

$remain_ = $stockBook[1]['REMAINING'];
Undefined offset: 1

again I tried

$remain_ = $stockBook->REMAINING;
Trying to get property 'REMAINING' of non-object

How can get the value of REMAINING attribute?

4
  • 1
    counting starts with 0 not 1. It would be $remain_ = $stockBook[0]['REMAINING']; or $remain_ = $stockBook[0]['#533']['REMAINING']; Commented Sep 13, 2018 at 10:40
  • @DieterKräutl getting error Cannot use object of type stdClass as array when I did $remain_ = $stockBook[0]['REMAINING']; Commented Sep 13, 2018 at 10:45
  • $stockBook = (array)$stockBook; Commented Sep 13, 2018 at 10:46
  • 1
    @user4221591 you are using laravel. So You should use laravel collection helper functions this will be very easy and also help us to minimize errors in out code. you can use this anwer https://stackoverflow.com/a/52312233/10288451 Commented Sep 13, 2018 at 10:59

5 Answers 5

2

if you have only single object inside array then you can use this

$arr = array:1 [▼
            0 => {#533 ▼
                "BOOKID": "1"
                "REMAINING": 17.0
            }
        ]

$object = collect($arr);
$item = $object->first();
dd($item->REMAINING);

But if you have multiple objects inside array then use this

$arr = array:1 [▼
            0 => {#533 ▼
                "BOOKID": "1"
                "REMAINING": 17.0
            },
            1 => {#533 ▼
                "BOOKID": "1"
                "REMAINING": 17.0
            }
        ]
$object = collect($arr);
$list = $object->pluck("REMAINING");
dd($list);

By using above example you will get list of all the Remainings from array object.

Hope this will help you.

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

2 Comments

@user4221591 if you think this helpful for you then please mark verified. That will be appreciated.
Collections are very convenient, but this answer will not help to understand difference between array and objects :) @user4221591 better read my answer, but still use this one ;)
0

Array numeration starts with 0 so $array[0] is the first element, not $array[1].

$stockBook = (array)$stockBook;
$remain_ = $stockBook[0]['REMAINING'];

4 Comments

Why cast $stockBook to an array when it, according to the var_dump already is an array?
Because of his Comment -> @DieterKräutl getting error Cannot use object of type stdClass as array when I did $remain_ = $stockBook[0]['REMAINING']; – user4221591 12 mins ago
Well, that explains it ;)
still didn't get why you should cast it to array? what if there will be 10 mlns of records? its possible to do without casting. Please check my answer. ;)
0

Try this:

$stockBook[0]->REMAINING;

If you want to retrieve element from array use []

If you want to retrieve element from object use ->

In your example we see that you have array of php objects, then you should use both.

Comments

0
    foreach((array)$stockBook as $key => $value){  // $stockBook or $stockBook[0]
        var_dump($key, $value);
    }
    // or
    var_dump($stockBook->BOOKID, $stockBook->REMAINING); // $stockBook or $stockBook[0]

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

If this is your data structure

array:1 [▼
  0 => {#533 ▼
    +"BOOKID": "1"
    +"REMAINING": 17.0
  }
]

and this is what you do

$remain_ = $stockBook[1]['REMAINING'];
    Undefined offset: 1

Then the error message explain the first part of your problem. You are trying to access the element with index 1 in an array that only has one element with index 0. Arrays are zero-based. That means, the first element is 0, the second is 1 etc. So you are trying to access an element that doesn't exist.

The second issue, according to your comment

Cannot use object of type stdClass as array when I did $remain_ = $stockBook[0]['REMAINING'];

indicates that each element in the array is an object. So to get the REMAINING attribute of the object at index 0 in the array you could do.

echo $stockBook[0]->REMAINING; // would print "17.0"

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.