douya1248 2019-02-04 10:35
浏览 54
已采纳

在php中无法访问关联数组

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 Stack trace: #0 /var/www/html/include/Dauerreservierung/checkForOverlapWithExistingRequestsOrReservations.php(5): mainframe(Array) #1 {main} 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"
    }
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2019-02-04 10:46
    关注

    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 ->.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里