如何将多维数组拆分为简单数组

时间:2017-11-11 20:30:35

标签: php arrays multidimensional-array

我这样的多维数组是用户做出的选择列表。这只是从用户检索的数据的一个示例。子阵列的组合可以根据用户选择而变化。 类似于分别具有元素3,5,2的3个子阵列。数组键也需要来自...

array(7) {
    ["resume_residence"]=>
    string(8) "Barbados"
    ["resume_language_fluently"]=>
    string(8) "Mandarin"
    ["resume_language_professional"]=>
    string(7) "Spanish"
    ["resume_language_basic"]=>
    string(8) "Mandarin"
    ["resume_overseas_experience"]=>
    string(2) "No"
    ["resume_degree"]=>     array(2) {
        [0]=>   string(2) "BA"
        [1]=>   string(3) "BFA"
    }
    ["resume_career_employment_status"]=>   array(2) {
        [0]=>   string(8) "Employed"
        [0]=>   string(10) "Unemployed"
    }
}

我想将此数组转换为多个数组,每个数组都有单值键,如

array(4){
    array(7) {
        ["resume_residence"]=>    string(8) "Barbados"
        ["resume_language_fluently"]=>    string(8) "Mandarin"
        ["resume_language_professional"]=>    string(7) "Spanish"
        ["resume_language_basic"]=>    string(8) "Mandarin"
        ["resume_overseas_experience"]=>    string(2) "No"
        ["resume_degree"]=> string(2) "BA"
        ["resume_career_employment_status"]=> string(2) "Employed"
    },
    array(7) {
        ["resume_residence"]=>    string(8) "Barbados"
        ["resume_language_fluently"]=>    string(8) "Mandarin"
        ["resume_language_professional"]=>    string(7) "Spanish"
        ["resume_language_basic"]=>    string(8) "Mandarin"
        ["resume_overseas_experience"]=>    string(2) "No"
        ["resume_degree"]=> string(2) "BA"
        ["resume_career_employment_status"]=> string(2) "Unemployed"
    },
    array(7) {
        ["resume_residence"]=>    string(8) "Barbados"
        ["resume_language_fluently"]=>    string(8) "Mandarin"
        ["resume_language_professional"]=>    string(7) "Spanish"
        ["resume_language_basic"]=>    string(8) "Mandarin"
        ["resume_overseas_experience"]=>    string(2) "No"
        ["resume_degree"]=> string(2) "BFA"
        ["resume_career_employment_status"]=> string(2) "Employed"
    },
    array(7) {
        ["resume_residence"]=>    string(8) "Barbados"
        ["resume_language_fluently"]=>    string(8) "Mandarin"
        ["resume_language_professional"]=>    string(7) "Spanish"
        ["resume_language_basic"]=>    string(8) "Mandarin"
        ["resume_overseas_experience"]=>    string(2) "No"
        ["resume_degree"]=> string(2) "BFA"
        ["resume_career_employment_status"]=> string(2) "Unemployed"
    }
}

请帮助我如何将阵列拆分成多个阵列,如上图所示。

1 个答案:

答案 0 :(得分:0)

您似乎希望生成分配给输入中不同键的数组值的所有可能组合。

您可以使用递归执行此操作:

$array = [
    "resume_residence"=> "Barbados",
    "resume_language_fluently"=> "Mandarin",
    "resume_language_professional"=> "Spanish",
    "resume_language_basic"=> "Mandarin",
    "resume_overseas_experience"=> "No",
    "resume_degree"=> ["BA", "FBA"],
    "resume_career_employment_status"=> ["Employed", "Unemployed"]
];

function expandArrays(&$array) { // Argument by reference
    // Get current key and value based on the array pointer
    $key = key($array);
    $value = current($array);
    // Get results for any further key/values through recursion:
    $results = next($array) ? expandArrays($array) : [$array];
    // Nothing changes if the current value is not an array
    if (!is_array($value)) return $results;
    // It is an array, so produce all combinations of the partial results with each value
    foreach($value as $v) {
        foreach($results as $r) {
            $r[$key] = $v;
            $newResults[] = $r;
        }
    }
    // ... and return that as a result
    return $newResults;
}

$results = expandArrays($array);

print_r($results);

输出:

Array (
    [0] => Array (
        [resume_residence] => Barbados
        [resume_language_fluently] => Mandarin
        [resume_language_professional] => Spanish
        [resume_language_basic] => Mandarin
        [resume_overseas_experience] => No
        [resume_degree] => BA
        [resume_career_employment_status] => Employed
    )
    [1] => Array (
        [resume_residence] => Barbados
        [resume_language_fluently] => Mandarin
        [resume_language_professional] => Spanish
        [resume_language_basic] => Mandarin
        [resume_overseas_experience] => No
        [resume_degree] => BA
        [resume_career_employment_status] => Unemployed
    )
    [2] => Array (
        [resume_residence] => Barbados
        [resume_language_fluently] => Mandarin
        [resume_language_professional] => Spanish
        [resume_language_basic] => Mandarin
        [resume_overseas_experience] => No
        [resume_degree] => FBA
        [resume_career_employment_status] => Employed
    )
    [3] => Array (
        [resume_residence] => Barbados
        [resume_language_fluently] => Mandarin
        [resume_language_professional] => Spanish
        [resume_language_basic] => Mandarin
        [resume_overseas_experience] => No
        [resume_degree] => FBA
        [resume_career_employment_status] => Unemployed
    )
)