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 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)