2

I am using Laravel and Mysql for my back end development. I have a query that prints userid,score and district. The query in Laravel query builder form

$districtRank=DB::table('scores')
                ->join('users','users.id','=','scores.user_id')
                ->select('scores.score','users.id','users.district')
                ->orderBy('scores.score','DESC')
                ->get()->toArray();

The data i receive when i dd($districtRank) is

array:9 [
0 => {#808
  +"score": 40
  +"id": 3
  +"district": "Vavuniya"
}
1 => {#809
  +"score": 38
  +"id": 20
  +"district": "Vavuniya"
}
2 => {#810
  +"score": 36
  +"id": 22
  +"district": "Vavuniya"
}
3 => {#811
  +"score": 30
  +"id": 3
  +"district": "Vavuniya"
}
4 => {#812
  +"score": 30
  +"id": 12
  +"district": "Vavuniya"
}
5 => {#813
  +"score": 29
  +"id": 21
  +"district": "Vavuniya"
}
6 => {#814
  +"score": 25
  +"id": 15
  +"district": "Kegalle"
}
7 => {#815
  +"score": 20
  +"id": 18
  +"district": "Kalutara"
}
8 => {#816
  +"score": 12
  +"id": 23
  +"district": "Vavuniya"
}

]

Var_dump($districtRank)

array(9) {
  [0]=>
  object(stdClass)#808 (3) {
    ["score"]=>
    int(40)
    ["id"]=>
    int(3)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [1]=>
  object(stdClass)#809 (3) {
    ["score"]=>
    int(38)
    ["id"]=>
    int(20)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [2]=>
  object(stdClass)#810 (3) {
    ["score"]=>
    int(36)
    ["id"]=>
    int(22)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [3]=>
  object(stdClass)#811 (3) {
    ["score"]=>
    int(30)
    ["id"]=>
    int(3)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [4]=>
  object(stdClass)#812 (3) {
    ["score"]=>
    int(30)
    ["id"]=>
    int(12)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [5]=>
  object(stdClass)#813 (3) {
    ["score"]=>
    int(29)
    ["id"]=>
    int(21)
    ["district"]=>
    string(8) "Vavuniya"
  }
  [6]=>
  object(stdClass)#814 (3) {
    ["score"]=>
    int(25)
    ["id"]=>
    int(15)
    ["district"]=>
    string(7) "Kegalle"
  }
  [7]=>
  object(stdClass)#815 (3) {
    ["score"]=>
    int(20)
    ["id"]=>
    int(18)
    ["district"]=>
    string(8) "Kalutara"
  }
  [8]=>
  object(stdClass)#816 (3) {
    ["score"]=>
    int(12)
    ["id"]=>
    int(23)
    ["district"]=>
    string(8) "Vavuniya"
  }
}

how do i access the array elements I have already tried using

foreach($districtRank as $item){
                    $id=$item[0];
                    }

as well as

foreach($districtRank as $item){
                    $id=$item["id"];
                    }

Of which both gives me an error

Cannot use object of type stdClass as array

why is this happening and how do i fix it.

2
  • it already says Cannot use object of type stdClass as array, it means its not an array, but an object -> use the arrow notation Commented Mar 29, 2018 at 3:42
  • Possible duplicate of Convert stdClass object to array in PHP Commented Mar 29, 2018 at 6:48

4 Answers 4

2

Because object and array is different (array of objects != array)

Try using -> operator instead:

foreach ($districtRank as $item){
    $id = $item->id;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have added my var_dump in the question now
If edit to $districtRank=DB::table(...)->get(); and using my foreach above, you can show result or error?
Sorry my bad it was successful and gives me the correct value. is there any way to convert it into an array instead of accessing object elements
@NaveedSheriffdeen: You can see this answer, hope it helps! stackoverflow.com/questions/19272011/…
1

Your $item is not an array. Try using ...

$item->id

Comments

0

use some specific foreach argument to have better result

foreach($district$ank as $key => $value){
$id = $value['id'];
}

Comments

0

You should try this:

foreach ($districtRank as $key => $item){
    $id = $item[$key]->id;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.