chunk array by key gap

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












I have an array from array_diff function and it looks like below:



Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)


As you can see we have a gap between 3 and 5 keys (Key # 4).
How can I split that array into 2 parts (may be more if the're more gaps).
So the final output will be:



Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)


Thanks!










share|improve this question

















  • 1




    how that gap length is determined. is it varying number or constant?
    – prasanna puttaswamy
    2 hours ago






  • 1




    It's a varying number
    – user889349
    2 hours ago














up vote
6
down vote

favorite
1












I have an array from array_diff function and it looks like below:



Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)


As you can see we have a gap between 3 and 5 keys (Key # 4).
How can I split that array into 2 parts (may be more if the're more gaps).
So the final output will be:



Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)


Thanks!










share|improve this question

















  • 1




    how that gap length is determined. is it varying number or constant?
    – prasanna puttaswamy
    2 hours ago






  • 1




    It's a varying number
    – user889349
    2 hours ago












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





I have an array from array_diff function and it looks like below:



Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)


As you can see we have a gap between 3 and 5 keys (Key # 4).
How can I split that array into 2 parts (may be more if the're more gaps).
So the final output will be:



Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)


Thanks!










share|improve this question













I have an array from array_diff function and it looks like below:



Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)


As you can see we have a gap between 3 and 5 keys (Key # 4).
How can I split that array into 2 parts (may be more if the're more gaps).
So the final output will be:



Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)


Thanks!







php array-difference






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









user889349

1,35522341




1,35522341







  • 1




    how that gap length is determined. is it varying number or constant?
    – prasanna puttaswamy
    2 hours ago






  • 1




    It's a varying number
    – user889349
    2 hours ago












  • 1




    how that gap length is determined. is it varying number or constant?
    – prasanna puttaswamy
    2 hours ago






  • 1




    It's a varying number
    – user889349
    2 hours ago







1




1




how that gap length is determined. is it varying number or constant?
– prasanna puttaswamy
2 hours ago




how that gap length is determined. is it varying number or constant?
– prasanna puttaswamy
2 hours ago




1




1




It's a varying number
– user889349
2 hours ago




It's a varying number
– user889349
2 hours ago












3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-



<?php

$arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');

$new_array = ;
$old_key = -1;
$i = 0;
foreach($arr as $key=>$val)
if(($key-$old_key) ==1)
$new_array[$i] = $val;
$old_key = $key;

if(($key-$old_key) >1)
$i++;
$new_array[$i] = $val;
$old_key = $key;


print_r($new_array);


https://3v4l.org/Yl9rp






share|improve this answer



























    up vote
    2
    down vote













    This should also work -



    $arr = [
    0 => 'world',
    1 => 'is',
    2 => 'a',
    3 => 'wonderfull',
    5 => 'in',
    6 => 'our',
    ];
    // Extract keys
    $keys = array_keys($arr);
    // Generate expected keys
    $temp = range($keys[0], end($keys));
    // Find missing keys
    $missing = array_diff($temp, $keys);
    // Fetch first missing keys
    $missing = array_shift($missing);
    // Split array depending on missing key
    $final = array_chunk($arr, $missing);

    var_dump($final);


    Output



    array(2) 
    [0]=>
    array(4)
    [0]=>
    string(5) "world"
    [1]=>
    string(2) "is"
    [2]=>
    string(1) "a"
    [3]=>
    string(10) "wonderfull"

    [1]=>
    array(2)
    [0]=>
    string(2) "in"
    [1]=>
    string(3) "our"







    share|improve this answer
















    • 1




      It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
      – EPB
      2 hours ago











    • @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
      – Alive to Die
      1 hour ago

















    up vote
    1
    down vote













    You can make use of the array internal pointer to traverse the array.



    <?php
    $arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
    print_r($arr);

    $result = Array();
    $lastkey;
    while($word = current($arr))

    $key = key($arr);
    if(isset($lastkey) && $key == $lastkey + 1)

    $result[count($result) - 1] = $word;

    else

    $result = Array($word);

    $lastkey = $key;
    next($arr);

    print_r($result);
    ?>





    share|improve this answer




















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53038968%2fchunk-array-by-key-gap%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-



      <?php

      $arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');

      $new_array = ;
      $old_key = -1;
      $i = 0;
      foreach($arr as $key=>$val)
      if(($key-$old_key) ==1)
      $new_array[$i] = $val;
      $old_key = $key;

      if(($key-$old_key) >1)
      $i++;
      $new_array[$i] = $val;
      $old_key = $key;


      print_r($new_array);


      https://3v4l.org/Yl9rp






      share|improve this answer
























        up vote
        5
        down vote



        accepted










        You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-



        <?php

        $arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');

        $new_array = ;
        $old_key = -1;
        $i = 0;
        foreach($arr as $key=>$val)
        if(($key-$old_key) ==1)
        $new_array[$i] = $val;
        $old_key = $key;

        if(($key-$old_key) >1)
        $i++;
        $new_array[$i] = $val;
        $old_key = $key;


        print_r($new_array);


        https://3v4l.org/Yl9rp






        share|improve this answer






















          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-



          <?php

          $arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');

          $new_array = ;
          $old_key = -1;
          $i = 0;
          foreach($arr as $key=>$val)
          if(($key-$old_key) ==1)
          $new_array[$i] = $val;
          $old_key = $key;

          if(($key-$old_key) >1)
          $i++;
          $new_array[$i] = $val;
          $old_key = $key;


          print_r($new_array);


          https://3v4l.org/Yl9rp






          share|improve this answer












          You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-



          <?php

          $arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');

          $new_array = ;
          $old_key = -1;
          $i = 0;
          foreach($arr as $key=>$val)
          if(($key-$old_key) ==1)
          $new_array[$i] = $val;
          $old_key = $key;

          if(($key-$old_key) >1)
          $i++;
          $new_array[$i] = $val;
          $old_key = $key;


          print_r($new_array);


          https://3v4l.org/Yl9rp







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Alive to Die

          54.5k82767




          54.5k82767






















              up vote
              2
              down vote













              This should also work -



              $arr = [
              0 => 'world',
              1 => 'is',
              2 => 'a',
              3 => 'wonderfull',
              5 => 'in',
              6 => 'our',
              ];
              // Extract keys
              $keys = array_keys($arr);
              // Generate expected keys
              $temp = range($keys[0], end($keys));
              // Find missing keys
              $missing = array_diff($temp, $keys);
              // Fetch first missing keys
              $missing = array_shift($missing);
              // Split array depending on missing key
              $final = array_chunk($arr, $missing);

              var_dump($final);


              Output



              array(2) 
              [0]=>
              array(4)
              [0]=>
              string(5) "world"
              [1]=>
              string(2) "is"
              [2]=>
              string(1) "a"
              [3]=>
              string(10) "wonderfull"

              [1]=>
              array(2)
              [0]=>
              string(2) "in"
              [1]=>
              string(3) "our"







              share|improve this answer
















              • 1




                It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
                – EPB
                2 hours ago











              • @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
                – Alive to Die
                1 hour ago














              up vote
              2
              down vote













              This should also work -



              $arr = [
              0 => 'world',
              1 => 'is',
              2 => 'a',
              3 => 'wonderfull',
              5 => 'in',
              6 => 'our',
              ];
              // Extract keys
              $keys = array_keys($arr);
              // Generate expected keys
              $temp = range($keys[0], end($keys));
              // Find missing keys
              $missing = array_diff($temp, $keys);
              // Fetch first missing keys
              $missing = array_shift($missing);
              // Split array depending on missing key
              $final = array_chunk($arr, $missing);

              var_dump($final);


              Output



              array(2) 
              [0]=>
              array(4)
              [0]=>
              string(5) "world"
              [1]=>
              string(2) "is"
              [2]=>
              string(1) "a"
              [3]=>
              string(10) "wonderfull"

              [1]=>
              array(2)
              [0]=>
              string(2) "in"
              [1]=>
              string(3) "our"







              share|improve this answer
















              • 1




                It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
                – EPB
                2 hours ago











              • @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
                – Alive to Die
                1 hour ago












              up vote
              2
              down vote










              up vote
              2
              down vote









              This should also work -



              $arr = [
              0 => 'world',
              1 => 'is',
              2 => 'a',
              3 => 'wonderfull',
              5 => 'in',
              6 => 'our',
              ];
              // Extract keys
              $keys = array_keys($arr);
              // Generate expected keys
              $temp = range($keys[0], end($keys));
              // Find missing keys
              $missing = array_diff($temp, $keys);
              // Fetch first missing keys
              $missing = array_shift($missing);
              // Split array depending on missing key
              $final = array_chunk($arr, $missing);

              var_dump($final);


              Output



              array(2) 
              [0]=>
              array(4)
              [0]=>
              string(5) "world"
              [1]=>
              string(2) "is"
              [2]=>
              string(1) "a"
              [3]=>
              string(10) "wonderfull"

              [1]=>
              array(2)
              [0]=>
              string(2) "in"
              [1]=>
              string(3) "our"







              share|improve this answer












              This should also work -



              $arr = [
              0 => 'world',
              1 => 'is',
              2 => 'a',
              3 => 'wonderfull',
              5 => 'in',
              6 => 'our',
              ];
              // Extract keys
              $keys = array_keys($arr);
              // Generate expected keys
              $temp = range($keys[0], end($keys));
              // Find missing keys
              $missing = array_diff($temp, $keys);
              // Fetch first missing keys
              $missing = array_shift($missing);
              // Split array depending on missing key
              $final = array_chunk($arr, $missing);

              var_dump($final);


              Output



              array(2) 
              [0]=>
              array(4)
              [0]=>
              string(5) "world"
              [1]=>
              string(2) "is"
              [2]=>
              string(1) "a"
              [3]=>
              string(10) "wonderfull"

              [1]=>
              array(2)
              [0]=>
              string(2) "in"
              [1]=>
              string(3) "our"








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 hours ago









              Sougata Bose

              26.2k43061




              26.2k43061







              • 1




                It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
                – EPB
                2 hours ago











              • @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
                – Alive to Die
                1 hour ago












              • 1




                It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
                – EPB
                2 hours ago











              • @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
                – Alive to Die
                1 hour ago







              1




              1




              It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
              – EPB
              2 hours ago





              It's worth noting this works if you only expect to have a single gap. If you have a second gap, they're not added to a third chunk. [Edit: And if the first chunk is longer than the second.]
              – EPB
              2 hours ago













              @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
              – Alive to Die
              1 hour ago




              @EPB is right. check here:-3v4l.org/1iVRg And result through my code:- 3v4l.org/aikEW
              – Alive to Die
              1 hour ago










              up vote
              1
              down vote













              You can make use of the array internal pointer to traverse the array.



              <?php
              $arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
              print_r($arr);

              $result = Array();
              $lastkey;
              while($word = current($arr))

              $key = key($arr);
              if(isset($lastkey) && $key == $lastkey + 1)

              $result[count($result) - 1] = $word;

              else

              $result = Array($word);

              $lastkey = $key;
              next($arr);

              print_r($result);
              ?>





              share|improve this answer
























                up vote
                1
                down vote













                You can make use of the array internal pointer to traverse the array.



                <?php
                $arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
                print_r($arr);

                $result = Array();
                $lastkey;
                while($word = current($arr))

                $key = key($arr);
                if(isset($lastkey) && $key == $lastkey + 1)

                $result[count($result) - 1] = $word;

                else

                $result = Array($word);

                $lastkey = $key;
                next($arr);

                print_r($result);
                ?>





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can make use of the array internal pointer to traverse the array.



                  <?php
                  $arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
                  print_r($arr);

                  $result = Array();
                  $lastkey;
                  while($word = current($arr))

                  $key = key($arr);
                  if(isset($lastkey) && $key == $lastkey + 1)

                  $result[count($result) - 1] = $word;

                  else

                  $result = Array($word);

                  $lastkey = $key;
                  next($arr);

                  print_r($result);
                  ?>





                  share|improve this answer












                  You can make use of the array internal pointer to traverse the array.



                  <?php
                  $arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
                  print_r($arr);

                  $result = Array();
                  $lastkey;
                  while($word = current($arr))

                  $key = key($arr);
                  if(isset($lastkey) && $key == $lastkey + 1)

                  $result[count($result) - 1] = $word;

                  else

                  $result = Array($word);

                  $lastkey = $key;
                  next($arr);

                  print_r($result);
                  ?>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Ricky Mo

                  94515




                  94515



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53038968%2fchunk-array-by-key-gap%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery