简体   繁体   中英

How to split multidimensional array into simple arrays

I've an like this which is list of choices made by the user. ,它是用户做出的选择的列表。 This is just an example of data retrieved from the user. The combination of sub arrays may vary as per user selection. Like 3 sub arrays having elements 3,5,2 respectively. And the arrays keys will also need to be from that ...

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"
    }
}

I want to with each having single value key like ,每个都具有单个值键,例如

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"
    }
}

Please help me how i can split the array into multiple arrays like the above one ..

You seem to want to generate all possible combinations of the array values assigned to the different keys in your input.

You can do this with recursion:

$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);

Output:

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
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM