在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

我有一个对象,其中有几个无名对象需要解析。 为了解决这个问题,我必须在遍历它时在不同的点上多次转换为数组。 在我的localhost环境中,在Windows机器上运行PHP7,我遍历了对象,然后在需要时将其转换为数组,并调出我知道的变量索引然后再次转换为数组,所有这些都在一行中。 p>

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

这种方法导致PHPStorm将其标记为错误,但在浏览器中运行良好。 但是,一旦我将此代码带到我在Amazon Linux环境中运行PHP版本5.3.29的实时环境中。 我得到了: p>

[服务器]目前无法处理此请求。 HTTP错误500 p>

但是,一旦我通过将转换分成不同的行来更改代码,它就可以正常工作。 p>

  $ item =  (数组)$ items [$ x]; 
 $ item =(array)$ item ['row'] [0]; 
  code>  pre> 
 
 

My 问题是 strong>:为什么第一种方法在我的localhost环境中工作但是一旦被带到实时环境就崩溃了? 我搜索了PHP7发布的笔记,因为虽然它可能是一个较新的功能,因为PHPStorm将其标记为错误,但无法找到解决此问题的任何内容。 p>

作为旁注, 我还在我的localhost中启用了以下扩展名: p>

  • extension = php_curl.dll li>
  • extension = php_mbstring.dll li>
  • 延长= php_mysqli.dll LI>
  • 延长= php_openssl.dll LI>
  • 延长= php_pdo_mysql.dll LI>
  • 延长= php_pdo_sqlite .dll li>
  • extension = php_sqlite3.dll li> ul> div>

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]);