0

Having array in input so formed:

Array
(
    [0] => 10
    [1] => -1
    [2] => -1
    [3] => -1
    [4] => -1
    [5] => 15
    [6] => 16
    [7] => 17
    [8] => -1
    [9] => -1
    [10] => 20
)

How I can split it for -1 value having as output something so:

Array
(
    [0] => 10
    [1] => Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
        )
    [2] => Array
        ( 
            [0] => 20
        )
)

The php explode() function works for string input. While here is something for array.

4
  • 1
    Why isn't 10 inside a separate array? Commented Nov 19, 2016 at 12:11
  • The values in this case are all integer. Commented Nov 19, 2016 at 12:15
  • Why i want split array for -1 value. Commented Nov 19, 2016 at 12:17
  • Are you just trying to remove any -1s from the array and leave it otherwise unchanged? It's not quite clear what you need due to the inconsistency in the output. Commented Nov 19, 2016 at 12:37

4 Answers 4

1
  1 <?php
  2     $array = [10, -1, -1, -1, -1, 15, 16, 17, -1, -1, 20];
  3     $tempArray = [];
  4     $result = [];
  5     foreach($array as $item)
  6     {
  7         if($item == -1)
  8         {
  9             if(count($tempArray))
 10             {
 11                 $result[] = $tempArray;
 12                 $tempArray = [];
 13             }
 14         }
 15         else
 16             $tempArray[] = $item;
 17 
 18     }
 19     if(count($tempArray))
 20     {
 21         $result[] = $tempArray;
 22     }
 23     echo json_encode($result);

and the result is,

~$ php cal.php 
[[10],[15,16,17],[20]]
Sign up to request clarification or add additional context in comments.

4 Comments

Almost, except in the desired output index 0 is a value, not an array.
yeah here is not sure, if there are more than one non -1 element at the begin of the array?
Work very fine! Thanks.
-1 element work as "delimiter" inside array. I done example with explode(). for string it split for give delimiter. For array delimiter in this case is -1.
1

Well here is solution for you.

$myAry = array(
    '0' => 10,
    '1' => -1,
    '2' => -1,
    '3' => -1,
    '4' => -1,
    '5' => 15,
    '6' => 16,
    '7' => 17,
    '8' => -1,
    '9' => -1,
    '10' => 20
);

$newAry = Array();

function splitArray($ary , $index){
    global $newAry;
    if($index == count($ary)){
        return $newAry;
    }
    if( $ary[$index] != -1 ){
        $chunk = array();
        while(  $index < count($ary) && $ary[$index] != -1 ){
            array_push($chunk , $ary[$index]);
            $index++;
        }
        array_push($newAry , $chunk);
        splitArray($ary , $index);

    } else {
        $index++;
        splitArray($ary , $index);
    }
}


splitArray($myAry , 0);
print_r($newAry);

output:

Array ( 
[0] => Array ( [0] => 10 ) 
[1] => Array ( [0] => 15 [1] => 16 [2] => 17 ) 
[2] => Array ( [0] => 20 ) 
      )

Comments

0

It's not exactly the same result, using array_unique and unset

$array = array_unique($array);

foreach ($array as $k=>$item) {
   if($item == '-1'){
      unset($array[$k]);
   }
}

Output

array(5) {
  [0]=>
  int(10)
  [5]=>
  int(15)
  [6]=>
  int(16)
  [7]=>
  int(17)
  [10]=>
  int(20)
}

1 Comment

Not need remove -1 value from array but split array for -1 value. For to be more right, think to explode() function. It work for string but suppose to have an array and not a string. The result should to be something as: explode ($delimiter, $array) but of course explode make an array splitting a string for delimeter. In this case (as i asked) explode make an array starting from an array splitting it for give value.
0

To avoid tedious result array element counting and keeping track of result indexes, only push references into the result array when there is no reference declared. When a -1 value is encountered, destroy the reference so that the next qualifying value is encountered, it goes into a new group. Demo

$array = [10, -1, -1, -1, -1, 15, 16, 17, -1, -1, 20];
$result = [];
foreach ($array as $v) {
    if ($v === -1) {
        unset($ref);
        continue;
    }
    if (!isset($ref)) {
        $result[] =& $ref;
    }
    $ref[] = $v;
}
var_export($result);

Output:

array (
  0 => 
  array (
    0 => 10,
  ),
  1 => 
  array (
    0 => 15,
    1 => 16,
    2 => 17,
  ),
  2 => 
  array (
    0 => 20,
  ),
)

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.