PHP split array based on value [duplicate]

2019-07-18 14:59发布

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.

6条回答
等我变得足够好
2楼-- · 2019-07-18 15:21
$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;

}
查看更多
相关推荐>>
3楼-- · 2019-07-18 15:21

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);
}
查看更多
Evening l夕情丶
4楼-- · 2019-07-18 15:22

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
        )

)
查看更多
萌系小妹纸
5楼-- · 2019-07-18 15:29
<?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>";
}
?>
查看更多
手持菜刀,她持情操
6楼-- · 2019-07-18 15:34
$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)
)
查看更多
7楼-- · 2019-07-18 15:38

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.

查看更多
登录 后发表回答