0

Here is an example array I want to split:

(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0)

How do I split it in 3 arrays like this?

(1,2,3,4,5,0) (6,7,0) (8,9,10,11,12,0)
5
  • 2
    It's not obvious from your question... why does it split where it does? Commented Dec 18, 2013 at 1:42
  • 1
    Yes please provide some more information, your question doesn't show any duplicate value to split at? Commented Dec 18, 2013 at 1:44
  • Where's the duplication part? Commented Dec 18, 2013 at 1:55
  • if my array (1,2,3,4,5,0,6,7,0,8,9,10,11,12,0) how to split array by 0 to be (1,2,3,4,5,0) (6,7,0) (8,9,10,11,12,0) Commented Dec 18, 2013 at 1:58
  • @user3110422 What if there's more than one duplication e.g. (1,2,3,4,0,1,2,3,4) heres 1,2,3,4 are duplicate too. or this won't happen? Commented Dec 18, 2013 at 2:11

4 Answers 4

1

Here's the solution. (Author said he wants to split at '0' instead of duplicate values)

function split_array_by_duplicate_values($a, $duplicate = 0)
{
    $c = array_intersect($a, array($duplicate));
    $r = array();
    $i = 0;
    foreach($c as $k => $v) {
        $l   = ++$k - $i;
        $r[] = array_slice($a, $i, $l);
        $i   = $k;
    }

    return $r;
}

$a = array(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0);

print_r(split_array_by_duplicate_values($a));

returns

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 0
        )

    [1] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 0
        )

    [2] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 10
            [3] => 11
            [4] => 12
            [5] => 0
        )

)
Sign up to request clarification or add additional context in comments.

8 Comments

looks nice but when i copy paste, i have blank browser...anything wrong??
I forgot to put return statement, my bad. Could you try again?
hmm what about if my array (1,1,1,1,1,0,1,1,0,1,1,1,1,1,0) how to split to be (1,1,1,1,1,0) (1,1,0) (1,1,1,1,1,0)
Do you want to always split at 0 regardless of other duplicate values e.g. 1?
I've updated my answer to split by 0 instead. Also, pls accept the answer if it's correct.
|
0

Try this:

$array = array(1, 2, 3, 0, 4, 5, 6);

$array_0 = array_splice($array, 0, 4);
$array_1 = array_splice($array, 4);

Comments

0

I think what you're looking for is array_splice.

$array=array(1,2,3,0,4,5,6);
$newarray=array_splice($array,4);
print_r($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3]=>0);
print_r($newarray); //Array( [0]=>4 [1]=>5 [2]=>6)

If you want to split an array into two arrays in one multidimensional array, you could do

$array=array(1,2,3,0,4,5,6);
$newarray=array_chunk($array,4); //returns [[1,2,3,0],[4,5,6]]

Comments

0

You should use array split to split the array as you would like

<?php
$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2);      // returns "c", "d", and "e"
$output = array_slice($input, -2, 1);  // returns "d"
$output = array_slice($input, 0, 3);   // returns "a", "b", and "c"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.