2014-12-12 3 views
0

에서 이전 값을 찾기 ". 교육적인 이유로PHP 내가이 배열을 다차원 배열

function find($needle, $array, $parent = NULL) 
{ 
//moves the pointer until it reaches the desired value 
    while (current($array) != $needle){ 
    //if current value is an array, apply this function recursively 
    if (is_array(current($array))){ 
     $subarray = current($array); 
    //passes the previous parent array 
     find($needle, $subarray, prev($array)); 
    } 
    //once it reaches the end of the array, end the execution 
    if(next($array) == FALSE){ 
     return; 
    } 
    } 
    //once the pointer points to $needle, run find_prev() 
    find_prev(prev($array), $parent); 
} 

function find_prev($prev, $parent = NULL) 
{ 
    // in case there is no previous value in array and there is a superior level 
    if (!$prev && $parent) { 
    find_prev($parent); 
    return; 
    } 

    // in case previous value is an array 
    // find last value of that array 

    if (is_array($prev) && $prev){ 
    find_prev(end($prev), $parent)); 
    return; 
    } else { 
    $GLOBALS['pre'] = $prev; 
    } 
} 

을 나는이 기능에 약간의 시간을 할애했기 때문에 당신은 왜보다는 작동하지 않습니다에 대한 힌트를 제공 할 수 있다면, 그것은 좋은 것 :

은 내가 가진 무엇 당신이 가질 수있는 다른 간단한 해결책.

+0

더 많은 정보를 제공해 주시면보다 신속하게 답변을 얻을 수 있습니다. 예를 들어, 몇 가지 입력/출력 조합을 보여줄 수 있습니다. –

+0

'prev ($ array)'와'find_prev()'에 대한 모든 호출 대신'find()'함수를 사용하여 이전에 처리 된 값을 유지하고 바늘이 발견되면 리턴합니다. – axiac

+0

각 기능의 시작 부분 (함수 이름과 함께)에서'$ prev'의 값을'echo()'하는 것으로 트랙에서 벗어나는 부분을 쉽게 발견 할 수 있습니다. – axiac

답변

0

무한 루프가 있습니다. 당신의 알고리즘은 당신이하고 싶은 것에 약간 복잡합니다. 변수에 이전 값을 유지하고 $needle을 찾을 때 반환해야합니다. 다음은 해당 코드입니다. 나는만큼 내가 할 수있는 등의 코드를 수정하지하려고 :

function find($needle, $array, $lastValue = NULL) 
{ 
    $previousValue = null; 

    //moves the pointer until it reaches the desired value 
    while (current($array) != FALSE) { 
    $value = current($array); 

    //if current value is an array, apply this function recursively 
    if (is_array($value)) { 
     $subarray = $value; 
     //passes the previous value as the last value for the embedded array 
     $value = find($needle, $subarray, $previousValue); 
     if ($value !== NULL) { 
     return $value; 
     } 
    } else if ($value === $needle) { 
     //returns the previous value of the current array 
     if ($previousValue !== NULL) { 
     return $previousValue; 
     //returns the last checked value of the parent array 
     } else if ($lastValue !== NULL) { 
     return $lastValue; 
     } else { 
     return; 
     } 
    } else { 
     $previousValue = $value; 
    } 

    next($array); 
    } 
} 

$test = array(
    "a" => "b", 
    "c" => array(
    "foo" => "bar", 
    "3" => "4" 
), 
    "e" => "f" 
); 

$result = find("bar", $test); 

if ($result === null) { 
    print('no previous value'); 
} else { 
    $GLOBALS['pre'] = $result; 
    print($GLOBALS['pre']); 
} 

당신은 알고리즘 이런 종류의 코드에 TDD을 시도 할 수 있습니다. 너를 도울 수있어.

0

함수 find()은 하위 배열에 대해 반복적으로 호출하지만 내부 호출이 무언가를 찾았는지 확인하지 않고 검색을 계속하지 않습니다. 따라서 find_prev()에 대한 첫 번째 호출은 $test['b']을 첫 번째 매개 변수로 사용합니다 ($test의 마지막 요소 인 prev()). $test['a']으로 실행될 것으로 예상됩니다.