我最近将fom php 5.2升级到5.6,并且有一些代码我无法修复:
//Finds users with the same ip- or email-address
function find_related_users($user_id) {
global $pdo;
//print_R($pdo);
//Let SQL do the magic!
$sth = $pdo->prepare('CALL find_related_users(?)');
$sth->execute(array($user_id));
//print_R($sth);
//Contains references to all users by id, to check if a user has already been processed
$users_by_id = array();
//Contains arrays of references to users by depth
$users_by_depth = array();
while ($row = $sth->fetchObject()) {
//Create array for current depth, if not present
if (!isset($users_by_depth[$row->depth]))
$users_by_depth[$row->depth] = array();
//If the user is new
if (!isset($users_by_id[$row->id])) {
//Create user array
$user = array(
'id' => $row->id,
'name' => $row->name,
'email' => $row->email,
'depth' => $row->depth,
'adverts' => array()
);
//Add all users to depth array
@array_push($users_by_depth[$row->depth], &$user);
//Add references to all users to id array (necessary to check if the id has already been processed)
$users_by_id[$row->id] = &$user;
}
//If user already exists
else
$user = &$users_by_id[$row->id];
//Add advert to user
if ($row->advert_id != null)
array_push($user['adverts'], array(
'id' => $row->advert_id,
'title' => $row->advert_title,
'msgs' => $row->msgs,
'url' => $row->url
));
#print_r($user);
//Unset $user variable !!!
//If this is missing, all references in the array point to the same user
unset($user);
}
//Return users, grouped by depth
return $users_by_depth;
}
如果仅删除美元符号前的与号,该功能将停止按预期工作.从关于*的其他问题中,我发现这是通过引用进行的调用,对于新的php版本,它将停止.但是我找不到解决方案.
感谢您对如何为php 5.6.x更新此代码的任何帮助
解决方法:
您的代码可能永远无法正常工作,因为您正在抑制array_push()调用中的错误.请注意,只有array_push()的第一个参数通过引用传递,其他值始终按值传递.
您应该删除错误抑制器@(切勿在自己的代码中使用它),在这种情况下,您还可以执行以下操作:
$users_by_depth[$row->depth][] = &$user;
^^ add an element just like `array_push`
现在,您在$users_by_depth中的新值将包含对$user变量的引用.