我对php还是很陌生,我试图按用户的姓和名以升序对它们进行排序。我已经尝试过了,但是一直得到can't pass values by reference
。我该怎么做
foreach( $users->result() as $user ):
if ($user->first_name != '' && !$user->block):
$user_list[$user->id] = sort($user->first_name . '' . $user->last_name);
endif;
endforeach;
答案 0 :(得分:2)
您不能将字符串传递给方法sort()。我假设这是从数据库中获取的,在这种情况下,我通常会先使用SQL对其进行排序。
但是仍然可以实现您想要的。
$user_list=array();
foreach( $users->result() as $user ):
if ($user->first_name != '' && !$user->block):
$user_list[$user->id] = $user->first_name.' '.$user->last_name
endif;
endforeach;
sort($user_list);
答案 1 :(得分:0)
foreach( $users->result() as $user ) {
if ($user->first_name != '' && !$user->block) {
$user_list[$user->id] = $user->first_name . '' . $user->last_name;
}
}
$user_list = asort($user_list); //sorted array