Associative array not accessible in php

JSONBUG123

The situation is as follows:

There's a numerical array containing associative arrays. These associative arrays have 2 elements named "anfang" and "ende".

I'm now using a foreach loop to loop through the numerical array, and inside the foreach loop I want to access the associative elements, it looks like this

foreach($allReservationsOrRequestsByUser as $singleRequestOrReservationByUser){
   if(singleRequestOrReservationByUser["start"] > singleRequestOrReservationByUser["end"]{
   //do something
   }
}

Now, I'm getting the following error message into my apache2 error log

[Mon Feb 04 11:23:16.018026 2019] [:error] [pid 1947] [client 127.0.0.1:41342] PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/include/Dauerreservierung/checkForOverlapWithExistingRequestsOrReservations.php:19\nStack trace:\n#0 /var/www/html/include/Dauerreservierung/checkForOverlapWithExistingRequestsOrReservations.php(5): mainframe(Array)\n#1 {main}\n thrown in /var/www/html/include/Dauerreservierung/checkForOverlapWithExistingRequestsOrReservations.php on line 19, referer: http://localhost/view/dauerreservierung.php `

I don't really understand what the problem is. I've been doing this all the time on my backend in other places and it always worked. This is the first time I see this error. I also tried out var_dump on a single element, like this:

var_dump($allReservationsOrRequestsByUser[0]["anfang"];

And I get the same error. It seems like something is wrong with the array, but I don't understand what it is because I didn't really do anything much different from earlier.

EDIT:

The complete dump Im getting when outputting the full array is:

array(1) {
[0]=>
    object(stdClass)#2 (2) {
    ["anfang"]=>
    string(19) "2019-01-23 00:00:00"
    ["ende"]=>
    string(19) "2019-01-30 00:00:00"
    }
}
Michał Haracewiat

The exception message is self-explanatory. You're working on stdClass instead of associative arrays. Perhaps you're encoding JSON? Can you show us complete dump (var_dump($allReservationsOrRequestsByUser))?

Anyway, if this is stdClass, to get start or end property you need to use ->.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related