0

I am trying to split a multi dimensional array based on another array of values Something like array_chunk($multi_array, array(1,3,7)). Managed to find something related to this over here array_chunk_multi. Just that it doesn't work for multi dimensional array. Currently it splits them up correctly just that I get NULL to all values.

PHP

function array_chunk_array(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;
}   

$input_sub_arr = range('1', '6');
$input_sub_array = array();
foreach ($input_sub_arr as $answer) {
  $input_sub_array[] = 'answer-'.$answer;
}
$input_sub_answers = array();
foreach ($input_sub_array as $input_sub_answer) {
  $input_sub_answers[$input_sub_answer] = array('attributes' => array('correct'));
}
//var_dump($input_sub_answers);
$new_answer = array_chunk_array($input_sub_answers, array(4,2));
var_dump($new_answer);

CURRENT ARRAY

array(6) {
  [0]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
  }
  [1]=>
  array(2) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(1) {

    }
  }
  [2]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
  [3]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
  [4]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
  [5]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
}

EXPECTED OUTPUT

array(4) {
  [0]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
  }
  [1]=>
  array(2) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(1) {

    }
  }
  [2]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
  [3]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
}
array(2) {  
  [1]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {
      ["@attributes"]=>
      array(1) {

      }
    }
  }
  [2]=>
  array(3) {
    ["@attributes"]=>
    array(2) {

    }
    ["answerText"]=>
    array(2) {

    }
  }
}

CURRENT OUTPUT

array(2) {
  [0]=>
  array(4) {
    [0]=>
    NULL
    [1]=>
    NULL
    [2]=>
    NULL
    [3]=>
    NULL
  }
  [1]=>
  array(2) {
    [0]=>
    NULL
    [1]=>
    NULL
  }
}
5
  • array_chunk_array($input_sub_answers, array(4,2)); it should be array_chunk($input_sub_answers, array(4,2)); and array_chunk need integer but your are given an array as it parameter.array_chunk is mandatory to use here? Commented Nov 23, 2016 at 6:43
  • @ChoncholMahmud I am trying to create a custom function array_chunk_array the version that accept an array as second parameter instead of integer. Commented Nov 23, 2016 at 6:52
  • Where you create array_chunk_array function? could you add this functions functionality? Commented Nov 23, 2016 at 6:55
  • @ChoncholMahmud Added. Needs to react like array_chunk, just that like as I said before. Commented Nov 23, 2016 at 6:55
  • Why you are using array_unshift ? array_unshift need first value to prepand. I added 2 here is demo phpio.net/s/16n9 Commented Nov 23, 2016 at 7:02

1 Answer 1

1

What really should be used here is array_splice:

$vl = [1,1,1,1,2,2,3,3,3];
$sz = [4, 2, 3];
function array_chunk_array(array $values, array $sizes)
{
    $result = [];

    foreach ($sizes as $s) {
        $result[] = array_splice($values, 0, $s);
    }

    return $result;
}

echo'<pre>',print_r(array_chunk_array($vl, $sz)),'</pre>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.