0

I have an array like this:

array(
    '1' => 'Item 1',
    '2' => 'Item 2',
    '3' => 'Item 3',
    '4' => 'Item 4',
    '5' => 'Item 5',
    '6' => 'Item 6',
    '7' => 'Item 7',
    '8' => 'Item 8',
    '9' => 'Item 9'
);

I want to separate this into 3 arrays where keys 1,2,3 go into $array1,$array2,$array3 respectively and then keys 4,5,6 and then 7,8,9 go into $array1,$array2,$array3 respectively too.

So, the final output would be:

$array1 = array(
    '1' => 'Item 1',
    '2' => 'Item 4',
    '3' => 'Item 7'
);

$array2 = array(
    '1' => 'Item 2',
    '2' => 'Item 5',
    '3' => 'Item 8'
);

$array3 = array(
    '1' => 'Item 3',
    '2' => 'Item 6',
    '3' => 'Item 9'
);

Or, if they keys are preserved (and not 1,2,3 in each array as in my example) then that wouldn't matter either. Either way is fine.

0

4 Answers 4

2
foreach($arr as $key => $value) {
    $final_arr[$key % 3][] = $value;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Very clean and easy to understand. Only one thing, the key for each array items will be numerical and start at 0, which may be what the poster wants
This doesn't actually answer the OP's question, he wants 3 distinct arrays, not one array with a different key for each of the 3 sets.
@webnoob is correct, this does not do exactly to the letter what I asked. However, I have marked it as the correct answer because the code is so simple, clean and straightforward and the end result still achieves the goal I set out for which was to separate the data into 3 lists with the first item of each list being the first 3 keys in the original array, the second item in each list being the second 3 keys etc etc.
2

Sth like

$i=0;
foreach($array as $k=>$v) {
 ${"array".$i%3+1}[$k]=$v;
 $i++;
}

Comments

1
for($i = 0; $i < count($input); $i += 3) {
    ${"array".$i} = array_slice($input, 0 + $i, 3);
}

This will split your array into new array on every third element. You can also replace 3 with a variable to split on some other number.

Edit: oh I just noticed, you might want to offset $i to get the array names in correct order. Now it will be array3, array6, array9 etc

Comments

0
   Try This

          $first_arr = array();
                $second_arr = array();
                $third_arr = array();
                $first = 0;
                $second = 1;
                $third = 2;    
                foreach($array as $value)
                {
                    if ($first++ % 3 == 0)
                    $first_arr[] = $value;
                    if ($second++ % 3 == 0)
                    $second_arr[] = $value;
                    if ($third++ % 3 == 0)
                    $third_arr[] = $value;
                }

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.