You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

递归函数开发:将键为目录值为文件名的嵌套数组转为路径数组

Recursive Function to Convert Nested Array to Directory Paths

Got it, let's work through this. You need a recursive function that takes that nested associative array and converts it into a flat array of directory-style paths, where each path builds from the top-level keys all the way down to the final value. Here's how to do this in both PHP (since your array syntax looks PHP-like) and JavaScript, just in case you need it:

PHP Implementation

This function uses recursion to traverse each level of the array, building up path segments until it hits a non-array value (the leaf node), then adds the full path to the result array.

function arrayToPathArray($array, $currentPath = '', &$result = []) {
    foreach ($array as $key => $value) {
        // Build the next segment of the path
        $newPath = $currentPath ? $currentPath . '/' . $key : $key;
        
        if (is_array($value)) {
            // Recurse into the nested array with the updated path
            arrayToPathArray($value, $newPath, $result);
        } else {
            // Add the complete path (path + leaf value) to the result
            $result[] = $newPath . '/' . $value;
        }
    }
    return $result;
}

// Your original array
$originalArray = [
    'common' => [
        'dailyPlan' => [
            '2017-03-14' => 'some_url_14_mar',
            '2017-02-25' => 'some_url_25_feb',
            '2017-01-01' => 'some_url_1_jan',
        ],
        'dailyResults' => [
            '2017-04-19' => 'some_url2',
            '2020-02-22' => 'future_url',
        ],
    ]
];

// Generate the path array
$pathArray = arrayToPathArray($originalArray);

// Output the result
print_r($pathArray);

PHP Output

When you run this, you'll get exactly the paths you need:

Array
(
    [0] => common/dailyPlan/2017-03-14/some_url_14_mar
    [1] => common/dailyPlan/2017-02-25/some_url_25_feb
    [2] => common/dailyPlan/2017-01-01/some_url_1_jan
    [3] => common/dailyResults/2017-04-19/some_url2
    [4] => common/dailyResults/2020-02-22/future_url
)

JavaScript Implementation

If you're working in JS instead, here's an equivalent function that handles nested objects:

function arrayToPathArray(obj, currentPath = '', result = []) {
    // Iterate over each key-value pair in the object
    for (const [key, value] of Object.entries(obj)) {
        const newPath = currentPath ? `${currentPath}/${key}` : key;
        
        // Check if the value is a nested object (not an array or null)
        if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
            arrayToPathArray(value, newPath, result);
        } else {
            // Add the full path to the result array
            result.push(`${newPath}/${value}`);
        }
    }
    return result;
}

// Your original object
const originalObj = {
    common: {
        dailyPlan: {
            '2017-03-14': 'some_url_14_mar',
            '2017-02-25': 'some_url_25_feb',
            '2017-01-01': 'some_url_1_jan',
        },
        dailyResults: {
            '2017-04-19': 'some_url2',
            '2020-02-22': 'future_url',
        },
    }
};

// Generate the path array
const pathArray = arrayToPathArray(originalObj);

// Log the result
console.log(pathArray);

JavaScript Output

The JS function will log this array:

[
  "common/dailyPlan/2017-03-14/some_url_14_mar",
  "common/dailyPlan/2017-02-25/some_url_25_feb",
  "common/dailyPlan/2017-01-01/some_url_1_jan",
  "common/dailyResults/2017-04-19/some_url2",
  "common/dailyResults/2020-02-22/future_url"
]

Key Notes

  • Both functions use pass-by-reference (PHP) or mutable array accumulation (JS) to efficiently build the result without creating unnecessary intermediate arrays.
  • The recursion stops when it hits a non-array/non-object value, which is when we add the full path to the result.
  • The path segments are joined with / to match your requested format.

内容的提问来源于stack exchange,提问作者lenriq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:18:29