shan
shan

Reputation: 89

Split rows of data in half

my array is bellow

Array
(
    [1] => Array
        (
            [9] => 6
            [11] => 6
            [12] => 6
            [13] => 6
            [14] => 12
        )
[2] => Array
    (
        [4] => 8
        [8] => 8
        [13] => 13
    )
)

And I want output like below , can someone help how I do this i wish always i break array in 2 parts.

Array
(
    [1] => Array
        (
           [1] => Array( [9] => 6
                         [11] => 6 
                       )
            [2] => Array( [12] => 6
                          [13] => 6
                          [14] => 12
                       )
        )

[2] => Array
        (
             [1] => Array( [4] => 8
                           [8] => 8
                           )
             [2] => Array( [13] => 13
                           )
        )
)

Upvotes: 0

Views: 277

Answers (4)

Ceeee
Ceeee

Reputation: 1442

You can use array_slice

$res = [];
foreach($arr as $key => $value){

  $res[$key][1] = array_slice($value, 0, 2, true);
  $res[$key][2] = array_slice($value,2, null, true);    
}

var_dump($res);

output:

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [9] => 6
                    [11] => 6
                )
            [2] => Array
                (
                    [12] => 6
                    [13] => 6
                    [14] => 12
                )
        )
    [2] => Array
        (
            [1] => Array
                (
                    [4] => 8
                    [8] => 8
                )
            [2] => Array
                (
                    [13] => 13
                )
        )
)

Only PHP > 5.2.4 : changelog says: The default value of the length parameter was changed to NULL. A NULL length now tells the function to use the length of array. Prior to this version, a NULL length was taken to mean a zero length (nothing will be returned).

Upvotes: 1

Ravi Hirani
Ravi Hirani

Reputation: 6539

Use array_chunk and array_slice.

$res = [];
foreach($arr as $k => $v){

  $lastdata = array_slice($v, 2,NULL,true);
  $firstdata = array_chunk($v,2,true);

  $res[$k]['1'] = $firstdata[0];
  $res[$k]['2'] = $lastdata;

}

echo '<pre>'; print_r($res);

output:-

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [9] => 6
                    [11] => 6
                )

            [2] => Array
                (
                    [12] => 6
                    [13] => 6
                    [14] => 12
                )

        )

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

            [2] => Array
                (
                    [13] => 13
                )

        )

)

Hope it will help you :)

Upvotes: 1

dhruv jadia
dhruv jadia

Reputation: 1680

    $a=array('1'=>array('9'=>'6','11'=>'6','12'=>'6','13'=>'6','14'=>'12'),'2'=>array('4'=>'8','8'=>'8','13'=>'13'));
    $res = array();
    foreach($a as $k => $v){

      $lastdata = array_slice($v, 2);
      $firstdata = array_chunk($v,2,true);

      $res[$k]['1'] = $firstdata[0];
      $res[$k]['2'] = $lastdata;

    }

echo '<pre>'; print_r($res);

check screenshot

enter image description here

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

You can try this -

$new= array_map(function($v) {
    $temp = array_chunk($v, 2); // break in chunks of 2
    $final[]= array_shift($temp); // Take the first chunk out
    $second= array();
    foreach($temp as $t) {
        $second = array_merge($second, $t); // merge the rest chunks
    }
    if(!empty($second)) {
        $final[]= $second;
    }
    return $final;
}, $array);

Fiddle

Upvotes: 2

Related Questions