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.

Naveed Sheriffdeen
  • 940
  • 6
  • 18
  • 37
  • it already says `Cannot use object of type stdClass as array`, it means its not an array, but an object `->` use the arrow notation – Kevin Mar 29 '18 at 03:42
  • Possible duplicate of [Convert stdClass object to array in PHP](https://stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php) – Gaurav Dave Mar 29 '18 at 06:48

4 Answers4

2

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

Try using -> operator instead:

foreach ($districtRank as $item){
    $id = $item->id;
}
anhvietcr
  • 170
  • 2
  • 7
1

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

$item->id
0

use some specific foreach argument to have better result

foreach($district$ank as $key => $value){
$id = $value['id'];
}
Wahyu Fadzar
  • 116
  • 5
0

You should try this:

foreach ($districtRank as $key => $item){
    $id = $item[$key]->id;
}
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57