Custom Next Previous / Next Prev

$career = $career_obj->getCareerById($id);
$careers = $career_obj->getCareers();
$career_ids = array();
if(!empty($careers)){
$count = 0;
foreach ($careers as $c){
$count++;
$career_ids[$count] = $c->ID;
}
}

$current_id = $career->ID;
$current_position = array_search($current_id, $career_ids);
$first_position = 1;
$last_position = sizeof($career_ids);

if($current_position==$first_position && $current_position==$last_position){
$next_position = $current_position;
$prev_position = $current_position;
}elseif($current_position==$first_position){
$next_position = $current_position + 1;
$prev_position = $last_position;
}elseif($current_position==$last_position){
$next_position = $first_position;
$prev_position = $current_position - 1;
}else{
$next_position = $current_position + 1;
$prev_position = $current_position - 1;
}

$next_item = $career_obj->getCareerById($career_ids[$next_position]);
$prev_item = $career_obj->getCareerById($career_ids[$prev_position]);

$next_link = home_url('/careers-details/'.$next_item->slug);
$prev_link = home_url('/careers-details/'.$prev_item->slug);