簡體   English   中英

PHP遞歸函數bug?

[英]PHP recursive function bug?

我使這個函數在嵌套數組中搜索但是我對這個數組一直是null:

$arr3 = [
    'first' => 1,
    'second' => 2,
    'third' => [
        'fourth' => 4,
    ]
];

/**
 * returns the key for the first found value
 *
 * @param $needle
 * @param array $haystack
 * @return false|int|string
 */
function array_search_value($needle, array $haystack) {

    $result = null;
    $found = array_search($needle, $haystack);

    if ( $found !== false ) {
        // end the recursion
        $result = $found;

    } else {
        foreach ($haystack as $key => $item) {
            if (is_array($item)) {
                array_search_value($needle, $item);
            } else {
                continue;
            }
        }
    }

    return $result;
}

var_dump(array_search_value(4, $arr3));

我無法弄清楚我做錯了什么? var_dump()結果應為string "fourth"

如果你在遞歸過程中發現了你正在尋找的東西,你就不會把它存放在任何地方。 這是我推薦的方法:

$arr3 = [
    'first' => 1,
    'second' => 2,
    'third' => [
        'fourth' => 4,
    ]
];

/**
 * returns the key for the first found value
 *
 * @param $needle
 * @param array $haystack
 * @return null|array
 */
function array_search_value($needle, array $haystack) {

    $result = null;
    $found = array_search($needle, $haystack);

    if ( $found !== false ) {
        // end the recursion
        $result = [ $found ]; //Array will make sense in a bit

    } else {
        foreach ($haystack as $key => $item) {
            if (is_array($item)) {
                $found = array_search_value($needle, $item);
                if ($found !== null) {
                   return array_merge([$key],$found);
                }
            } else {
                continue;
            }
        }
    }

    return $result;
}

var_dump(array_search_value(4, $arr3));

返回數組的原因是子數組與主數組具有相同的鍵,因此您可以通過遞歸訪問返回的每個數組條目的數組索引來一致地檢索正確的鍵。

查看代碼: http//sandbox.onlinephpfunctions.com/code/085c949f660504010ed7ebb7a846e31b3a766d61

下面是一個示例,說明為什么可能需要返回數組:

如果你考慮數組:

$arr3 = [
    'a' => 1,
    'b' => 2,
    'c' => [
        'a' => 4,
    ],
    "d"=>[
        "a" => [
            "a" => 19    
        ]
    ]
];

如果您正在尋找4,而不是返回數組你會得到a ,但也將是明確的,因為a根陣列中含有1

http://sandbox.onlinephpfunctions.com/code/43c2f2dfa197400df1e5748e12f12e5346abed3e

如果有多個路徑,您可以修改上述內容以獲取導致給定結果的所有路徑。

function array_search_value_all($needle, array $haystack) {

    $result = [];
    $found = array_search($needle, $haystack);

    if ( $found !== false ) {
        // end the recursion
        $result[] = [ $found ]; //Array will make sense in a bit

    } else {
        foreach ($haystack as $key => $item) {
            if (is_array($item)) {
                $found = array_search_value($needle, $item);
                if ($found !== []) {
                   $result[] = array_merge([$key],$found);
                }
            } else {
                continue;
            }
        }
    }

    return $result;
}

array_search_value_all將返回導致該值的所有路徑的數組。

示例: http//sandbox.onlinephpfunctions.com/code/fa4f5274703abb221f171c6e3ace5529594cdc8c

您缺少對$result的遞歸調用的分配。 你需要改變:

if (is_array($item)) {
     array_search_value($needle, $item);

要:(注意這是未找到的值繼續搜索而不僅僅是返回)

if (is_array($item) ) {
    $i = array_search_value($needle, $item);
    if ($i)
        return $i;

只需返回array_search_value函數的遞歸調用:

    $arr3 = [
    'first' => 1,
    'second' => 2,
    'third' => [
        'fourth' => 4,
    ]
];

/**
 * returns the key for the first found value
 *
 * @param $needle
 * @param array $haystack
 * @return false|int|string
 */
function array_search_value($needle, array $haystack) {

    $result = null;
    $found = array_search($needle, $haystack);

    if ( $found !== false ) {
        // end the recursion
        $result = $found;

    } else {
        foreach ($haystack as $key => $item) {
            if (is_array($item)) {
                return array_search_value($needle, $item);
            } else {
                continue;
            }
        }
    }

    return $result;
}

var_dump(array_search_value(4, $arr3));

當遞歸調用函數時,必須確保將遞歸調用的返回值傳播回原始調用者:

    $arr3 = [
    'first' => 1,
    'second' => 2,
    'third' => [
        'fourth' => 4,
    ]
];

/**
 * returns the key for the first found value
 *
 * @param $needle
 * @param array $haystack
 * @return false|int|string
 */
function array_search_value($needle, array $haystack) {

    $result = null;
    $found = array_search($needle, $haystack);

    if ( $found !== false ) {
        // end the recursion
        $result = $found;

    } else {

        foreach ($haystack as $key => $item) {
            if (is_array($item)) { 
               $target = array_search_value($needle, $item);
                if ($target){
                    return $target;
                }
            } else {
                continue;
            }
        }

    }
    return $result;
}

var_dump(array_search_value(4, $arr3));
    $arr3 = [
    'first' => 1,
    'second' => 2,
    'third' => [
        'fourth' => 4,
    ]
];

/**
 * returns the key for the first found value
 *
 * @param $needle
 * @param array $haystack
 * @return false|int|string
 */
function array_search_value($needle, array $haystack) {

    $found = array_search($needle, $haystack);

    if ($found) {
        // put directly return here
        return $found;

    } else {
        foreach ($haystack as $key => $item) {
            if (is_array($item)) {
                return array_search_value($needle, $item);
            } else {
                continue;
            }
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM