user3467855 user3467855 - 18 days ago 5
PHP Question

Split array with array_chunk dynamically based on another array

I am trying to split an array in a dynamic way, like on the function

array_chunk
instead of the second parameter
int $size
to be allowed to add an array like
array different_sizes


$input_sub_arr = range('01', '15');
$input_sub_array = array();
foreach ($input_sub_arr as $answer) {
$input_sub_array[] = 'answer-'.$answer;
}
var_Dump($input_sub_array);

$new_answer = array_chunk($input_sub_array, array(5, 6 , 2));
var_dump($new_answer);


Thank you bradynpoulsen. Updated my questions. What about for multi dimensional arrays:

array(9) {
[0]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(21) "<![CDATA[data link]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[1]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(true)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[network]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[2]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(20) "<![CDATA[physical]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[3]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[session]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[4]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(16) "<![CDATA[bits]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[5]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(16) "<![CDATA[data]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[6]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(17) "<![CDATA[frame]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[7]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(18) "<![CDATA[packet]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[8]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(true)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[segment]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
}


EXPECTED OUTPUT

array(4) {
[0]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(21) "<![CDATA[data link]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[1]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(true)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[network]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[2]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(20) "<![CDATA[physical]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[3]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[session]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
}
array(5) {
[0]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(16) "<![CDATA[bits]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[1]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(16) "<![CDATA[data]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[2]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(17) "<![CDATA[frame]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[3]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(false)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(18) "<![CDATA[packet]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
[4]=>
array(3) {
["@attributes"]=>
array(2) {
["correct"]=>
bool(true)
["points"]=>
int(1)
}
["answerText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(19) "<![CDATA[segment]]>"
}
["stortText"]=>
array(2) {
["@attributes"]=>
array(1) {
["html"]=>
bool(false)
}
["@value"]=>
string(12) "<![CDATA[]]>"
}
}
}


Currently it splits them up correctly just that I get NULL to all values.

Answer

What would be the goal of even doing that? Are you saying you want 3 arrays created, one with 5 items, followed by one with 6, followed by one with 2?

function partition(array $values, array $sizes)
{
    $results = [];
    foreach ($sizes as $size) {
        $current = [];
        while (count($values) > 0 && count($current) < $size) {
            $current[] = array_unshift($values);
        }
        $results[] = $current;
    }
    return $results;
}

$result = partition($input_sub_arr, [5, 6, 2]);
// [['answer-01', 'answer-02', 'answer-03', 'answer-04', 'answer-05'], ['answer-06', 'answer-07', 'answer-08', 'answer-09', 'answer-10', 'answer-11'], ['answer-12', 'answer-13']]