4

Possible Duplicate:
How to split an array based on a certain value?

Here is an example array I want to split:

(0, 1 ,2 ,3, 0, 4, 5)

How do I split it in 2 array like this?

(0, 1 ,2 ,3) (0, 4, 5)

Basically I want to check if the value of an element is zero and then create an array of all the elements until I find another zero. I can't seem to come up with a solution to this.

1
  • 2
    array_keys() [with search value] then array_slice(), but i'm sure one one as a niftier idea Commented Dec 13, 2012 at 4:06

6 Answers 6

5
$array = array(0, 1 ,2 ,3, 0, 4, 5);
$result = array();
$index = -1;
foreach ($array as $number) {
    if ($number == 0) {
        $index++;
    }
    $result[$index][] = $number;
}
echo print_r($result, true);

You'll end with the following.

array(
    0 => array(0, 1, 2, 3),
    1 => array(0, 4, 5)
)
Sign up to request clarification or add additional context in comments.

1 Comment

If the array does not start with the search value, the first key in the result array would be -1 and not 0.
1
$x = -1;
$newArr = array();
$array = array(0,1,2,3,0,4,5,6);

foreach ($array as $key => $value) {
  if($value == 0) 
    $x++;

  $newArr[$x][] = $value;

}

Comments

0

You can use PHP explode to break up into String arrays for each appearance of 0, but I see now that you have an array, not a string. As suggested above now, you can use the inverse, PHP implode.

Comments

0
<?php
$a=array(0, 1 ,2 ,3, 0, 4, 5);
$b =array();
$k=-1;
foreach($a as $key=>$val)
{
    if($val==0)
    {
      $k++;  
    }

    $b[$k][]=$val;
}


echo"<pre>";



print_r($b[0]);

print_r($b[1]);


foreach($b as $key=>$val)
{
    echo"<pre>";
    echo $val;
    echo"</pre>";
}
?>

Comments

0

First concatenate the elements, then split using 0 and then separate them again by prefixing 0 e.g. below

$concatenatedStr = implode(',', $array);
$indx = 0;
$fragments = array();
$fragementStrings= explode("0,", $concatenatedStr);
foreach($fragementStrings as $fragementString) {
  //you get your fragements here 
  $fragments[$indx++][] = explode(",", "0,".$fragementString);
}

Comments

0

Similar to a comment, array_keys can be used to search for all keys containing the value you search for (zero in your case).

You then can loop over these positions and slice the array (see array_slice) into the parts.

Example (Demo):

$keys  = array_keys($array, 0);
$parts = [];
$last  = 0;

foreach ($keys as $key)
{
    $last !== $key 
        && $parts[] = array_slice($array, $last, $key - $last, true);    
    $last = $key;
}

$parts[] = isset($key) ? array_slice($array, $key, null, true) : $array;

This variant even preserves original keys, exemplary $parts output:

array(2) {
  [0] => array(4) {
    [0] => int(0)
    [1] => int(1)
    [2] => int(2)
    [3] => int(3)
  }
  [1] => array(3) {
    [4] => int(0)
    [5] => int(4)
    [6] => int(5)
  }
}

Another alternative could be similar to some other answers outlined. The following variant does preserve keys, too:

$array = [0, 1 ,2 ,3, 0, 4, 5];
$parts = [];
$index = 0;

foreach ($array as $key => $number)
{
    $number == 0 and $key and $index++;
    $parts[$index][$key] = $number;
}

Output (Demo):

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

    [1] => Array
        (
            [4] => 0
            [5] => 4
            [6] => 5
        )

)

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.