0

I have a multi-dimensional array, the structure is like this:

$arr1 = array(
0 => array(
    'name' => 'Jim',
    'awards' => array(
        0 => 'awards one',
        1 => 'awards two',
        2 => 'awards three',
        3 => 'awards four',
        4 => 'awards five',
        5 => 'awards six',
    ),
),
1 => array(
    'name' => 'Kate',
    'awards' => array(
        0 => 'awards one',
        1 => 'awards two',
        2 => 'awards three',
    ),
));

If one of the element's count of 'awards' greater than 4, it will being a new element and contain 'awards' element after the fifth. like this:

$arr2 = array(
0 => array(
    'name' => 'Jim',
    'awards' => array(
        0 => 'awards one',
        1 => 'awards two',
        2 => 'awards three',
        3 => 'awards four',
    ),
),
1 => array(
    'name' => 'Jim',
    'awards' => array(
        0 => 'awards five',
        1 => 'awards six',
    ),
),
2 => array(
    'name' => 'Kate',
    'awards' => array(
        0 => 'awards one',
        1 => 'awards two',
        2 => 'awards three',
    ),
));

I'm not good at algorithm. I looked for everywhere but I couldn't find the solution. can someone help me. Thanks in advance!

2
  • just try something first, just use a foreach Commented Mar 11, 2016 at 2:11
  • Thank for your comment. Yes, I'm on the case. But there is no progress. Commented Mar 11, 2016 at 2:20

1 Answer 1

1
<?php
$arr1 = array(
    array(
        'name' => 'Jim',
        'awards' => array(
            0 => 'awards one',
            1 => 'awards two',
            2 => 'awards three',
            3 => 'awards four',
            4 => 'awards five',
            5 => 'awards six',
        ),
    ),
    array(
        'name' => 'Kate',
        'awards' => array(
            0 => 'awards one',
            1 => 'awards two',
            2 => 'awards three',
        ),
));

$result = array();
foreach( $arr1 as $player ) {
    while ( count($player['awards']) > 0 ) {
        $result[] = array(
            'name' => $player['name'],
            'awards' => array_splice($player['awards'], 0, 4)
        );
    }
}
var_export($result);

prints

array (
  0 => 
  array (
    'name' => 'Jim',
    'awards' => 
    array (
      0 => 'awards one',
      1 => 'awards two',
      2 => 'awards three',
      3 => 'awards four',
    ),
  ),
  1 => 
  array (
    'name' => 'Jim',
    'awards' => 
    array (
      0 => 'awards five',
      1 => 'awards six',
    ),
  ),
  2 => 
  array (
    'name' => 'Kate',
    'awards' => 
    array (
      0 => 'awards one',
      1 => 'awards two',
      2 => 'awards three',
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank your VolkerK. You've helped me a lot.

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.