php 는 배열 의 특정 값 의 모든 아래 표 시 를 되 돌려 줍 니 다.

2467 단어 php
참고 문서    http://php.net/manual/zh/function.array-keys.php
첫 번 째 아래 표 array_search 
다음  array_keys 배합 선택 가능 한 매개 변수 searchvalue 완료 
eg:  

function showIndex($source_arr, $sum)
{
    $index_list = [];
    $pass_index_list = [];
    foreach ($source_arr as $index => $value) {

        // filter repeat index
        if (in_array($value, $pass_index_list)) {
            continue;
        }

        // search another index
        $diff = $sum - $value;
        $ano_index = array_keys($source_arr, $diff);

        if (!$ano_index) {
            continue;
        }

       foreach ($ano_index as $choose_index) {
           $index_list[] = [$index,$choose_index];
       }


        // set repeat index
        $pass_index_list[] = $diff;

    }

    return $index_list;
}


// run function
$source_arr = [49, 1, 2, 3, 50, 50,49,49, 51];
//$source_arr = [49, 1, 2, 3, 50, 51];
$sum = 99;

print_r(showIndex($source_arr, $sum));

좋은 웹페이지 즐겨찾기