我一直试图将一些遗留代码移到Symfony2中,但是已经遇到了困扰我的事情。我有一个功能
public function findSeatsOnFlights($allInfo, $flights, $seatCodes)
{
}
在旧版和新代码中,$ allInfo的格式相同,所以不必担心这个问题。在我的遗留代码中,如果我有var_dump $ flights和$ seatCodes,我会得到类似
的内容array(2) {
[0]=> string(3) "VS7"
[1]=> string(3) "VS8"
}
array(2) {
[0]=> string(1) "C"
[1]=> string(1) "D"
}
为了解决这个问题,我会做
public function findSeatsOnFlights($allInfo, $flights, $seatCodes){
$answer = array( );
foreach ( $flights as $flight )
{
$finfo = $allInfo[ $flight ];
$fseats = $finfo["seats"];
foreach ( $seatCodes as $code )
{
}
}
}
但是,使用Symfony2,我的var_dump $ flight和$ seatCodes是
array (size=2)
0 =>
object(stdClass)[405]
public '__CLASS__' => string 'Nick\AlertBundle\Entity\AvailabilityAlertFlightNumbers' (length=55)
public 'id' => int 52
public 'flightNumber' => string 'VS7' (length=3)
public 'availabilityAlert' => string 'Nick\AlertBundle\Entity\AvailabilityAlert' (length=42)
1 =>
object(stdClass)[406]
public '__CLASS__' => string 'Nick\AlertBundle\Entity\AvailabilityAlertFlightNumbers' (length=55)
public 'id' => int 53
public 'flightNumber' => string 'VS8' (length=3)
public 'availabilityAlert' => string 'Nick\AlertBundle\Entity\AvailabilityAlert' (length=42)
array (size=2)
0 =>
object(stdClass)[406]
public '__CLASS__' => string 'Nick\AlertBundle\Entity\AvailabilityAlertBookingClass' (length=54)
public 'id' => int 62
public 'classLetter' => string 'C' (length=1)
public 'availabilityAlert' => string 'Nick\AlertBundle\Entity\AvailabilityAlert' (length=42)
1 =>
object(stdClass)[404]
public '__CLASS__' => string 'Nick\AlertBundle\Entity\AvailabilityAlertBookingClass' (length=54)
public 'id' => int 63
public 'classLetter' => string 'D' (length=1)
public 'availabilityAlert' => string 'Nick\AlertBundle\Entity\AvailabilityAlert' (length=42)
那么我该如何处理呢?我尝试过这样的事情没有成功
foreach ( $flights as $flight )
{
$finfo = $allInfo[ $flight->getFlightNumber() ];
....
如果我这样做,我会
警告:非法偏移类型(500内部服务器错误)
那么,将代码更改为现在处理对象的最佳方法是什么?