에서 이전 값을 찾기 ". 교육적인 이유로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;
}
}
을 나는이 기능에 약간의 시간을 할애했기 때문에 당신은 왜보다는 작동하지 않습니다에 대한 힌트를 제공 할 수 있다면, 그것은 좋은 것 :
이
은 내가 가진 무엇 당신이 가질 수있는 다른 간단한 해결책.
더 많은 정보를 제공해 주시면보다 신속하게 답변을 얻을 수 있습니다. 예를 들어, 몇 가지 입력/출력 조합을 보여줄 수 있습니다. –
'prev ($ array)'와'find_prev()'에 대한 모든 호출 대신'find()'함수를 사용하여 이전에 처리 된 값을 유지하고 바늘이 발견되면 리턴합니다. – axiac
각 기능의 시작 부분 (함수 이름과 함께)에서'$ prev'의 값을'echo()'하는 것으로 트랙에서 벗어나는 부분을 쉽게 발견 할 수 있습니다. – axiac