dongling0519 2016-06-07 12:20
浏览 64
已采纳

在PHP中将一个转换对象的值转换为一行中的数组

I have an object which has several nameless objects within it that I need to parse. To address this I had to cast to an array multiple times at different points while traversing it. In my localhost environment, which runs PHP7 in a windows machine, I traversed the object then casted it to an array when needed and called out the index of the variables I knew were there and then casted to an array again, all in one line.

$item = ((array)((array)$items[$x])['row'][0]);

This approach caused PHPStorm to label it as an error but worked fine in the browser. However, once I took this code to my live environment which is running PHP Version 5.3.29 on an Amazon Linux environment. I got:

[server] is currently unable to handle this request. HTTP ERROR 500

However, once I changed the code by splitting the casts into different lines it worked just fine.

$item = (array)$items[$x];
$item = (array)$item['row'][0];

My question is: Why does the first method work on my localhost environment but crashes the page once taken to a live environment? I searched the released notes of PHP7 as I though it was likely a newer feature since PHPStorm labels it as an error, but couldn't find anything that addresses this.

as a side note, I also have the following extension enabled in my localhost:

  • extension=php_curl.dll
  • extension=php_mbstring.dll
  • extension=php_mysqli.dll
  • extension=php_openssl.dll
  • extension=php_pdo_mysql.dll
  • extension=php_pdo_sqlite.dll
  • extension=php_sqlite3.dll
  • 写回答

1条回答 默认 最新

  • dongxie45083 2016-06-21 16:55
    关注

    As mentioned by Mark, upgrading PHP to 5.4 will allow you to use your single line example:

    $item = ((array)((array)$items[$x])['row'][0]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教