PHP传递递增类型抛出一个变量

时间:2014-02-15 02:24:40

标签: php

如何传递增量类型抛出变量,如下面的示例:

function next_prev_elem($step_id, $array, $incre){
    foreach ($array as $i) {
        $incre.$step_id;
        foreach ($array as $item) {
            if($step_id === $item['id']){
                return $step_id;
                break;
            }
        }
    }
    return false;
}
$prev_id = next_prev_elem($id, $page_news, '--');
$next_id = next_prev_elem($id, $page_news, '++');

2 个答案:

答案 0 :(得分:0)

function next_prev_elem($step_id, $array, $incre){
    foreach ($array as $i) {

        switch( $incre ) {
          case '++':
            $step_id++;
            break;
          case '--':
            $step_id--;
            break;
        }

        foreach ($array as $item) {
            if($step_id === $item['id']){
                return $step_id;
                break;
            }
        }
    }
    return false;
}

$prev_id = next_prev_elem($id, $page_news, '--');
$next_id = next_prev_elem($id, $page_news, '++');

答案 1 :(得分:0)

感谢@Marc B,我最终得到了这个解决方案:

function next_prev_elem($step_id, $array, $step){
    foreach ($array as $i) {
        $step_id = $step_id + $step;
        foreach ($array as $item) {
            if($step_id === $item['id']){
                return $step_id;
                break;
            }
        }
    }
    return false;
}
$prev_id = next_prev_elem($id, $page_news, -1);
$next_id = next_prev_elem($id, $page_news, 1);