chunk array by key gap
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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
add a comment |Â
up vote
6
down vote
favorite
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
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
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
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
php array-difference
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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"
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
add a comment |Â
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);
?>
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 2 hours ago


Alive to Die
54.5k82767
54.5k82767
add a comment |Â
add a comment |Â
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"
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
add a comment |Â
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"
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
add a comment |Â
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"
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"
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
add a comment |Â
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
add a comment |Â
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);
?>
add a comment |Â
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);
?>
add a comment |Â
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);
?>
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);
?>
answered 2 hours ago


Ricky Mo
94515
94515
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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