1

I am trying to split array data into multiple arrays based on change in data value at known position (column).

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

Here column 3 changes values from 3 to 4 and expected result is to have 2 arrays

$out1 = array(array(1,2,3),array(4,5,3));
$out2 = array(array(7,8,4), array(9,10,4), array(11,12,4));

since number of rows are variable, cannot use array_chunk since column 3 values are variable, cannot use array_filter number of output arrays are also variable. trying splice but failing...

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
prac
  • 41
  • 1
  • 5
  • Possible duplicate of [PHP splitting array into two arrays based on value](https://stackoverflow.com/questions/20098268/php-splitting-array-into-two-arrays-based-on-value) – Hearner Aug 01 '19 at 07:33

2 Answers2

2

You can use array_reduce to make new array, where index will be equal to last numbers in items

$new = array_reduce($input, function($c, $x) {  $c[$x[2]][] = $x; return $c; }, [] );
$out1 = $new[3];
$out2 = $new[4];

demo

But if array is not sorted and you want to split at points of changing that number, the code can be

$i = -1;
$last = null;
$new = [];
foreach($input as $x) {
    if ($x[2] != $last) {
        $i++;
        $last = $x[2];
    }
    $new[$i][] = $x;
}

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
  • Yes this works for the regular array as in the question. but not for an array with key=>values like $a = array( array('k1'=>1,'k2'=>2,'k3'=>3), array('k1'=>4,'k2'=>5,'k3'=>3), array('k1'=>7,'k2'=>8,'k3'=>4), array('k1'=>9,'k2'=>10,'k3'=>4) ); where i get Undefined offset: 2 could you clarify – prac Aug 02 '19 at 07:02
  • Change $x2 with end($x) – splash58 Aug 02 '19 at 07:06
  • { $c[end($x)] = $x; return $c; } outputs only the last arrays of each set. demo at . http://sandbox.onlinephpfunctions.com/code/9fa0a51574a8106847f88ee5b0629d43c5050c92 – prac Aug 02 '19 at 07:27
  • You should add empty square brackets as in my code (i can't write it on this phone :)) – splash58 Aug 02 '19 at 07:45
  • http://sandbox.onlinephpfunctions.com/code/e9fa14ae64f28cd8ad4fd809438b686ab863f48f – splash58 Aug 02 '19 at 08:18
0

You can use split index with index of array,

$out1 = $out2 = [];
$splitIndex = 2;
foreach($input as $k => $v){
    if($k < $splitIndex){
        $out1[] = $v;
    }else{
        $out2[] = $v;
    }
}
print_r($out1);
print_r($out2);

Working demo

Output:-

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

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

)
Array
(
    [0] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 4
        )

    [1] => Array
        (
            [0] => 9
            [1] => 10
            [2] => 4
        )

    [2] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 4
        )

)
Rahul
  • 18,271
  • 7
  • 41
  • 60