我最近將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;
}
如果僅刪除美元符號前的與號,該功能將停止按預期工作.從關于stackoverflow的其他問題中,我發現這是通過引用進行的調用,對于新的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變量的引用.
標簽:variables,reference,php
來源: https://codeday.me/bug/20191120/2041320.html