stay with the last 15 elements of an array [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • PHP get the last 3 elements of an array

    4 answers




I have a database on couchdb, each document has a slightly complex structure, here is a small part:



   "category": {
"1496805145": -1,
"1497064141": -1,
"1497150993": -1,
"1497322542": -1,
"1497487102": -1,
"1497571701": -1,
"1497657026": -1,
"1497749178": -1,
"1497920895": -1,
"1498005644": -1,
"1498091037": -1,
"1498179039": -1,
"1502183511": -1,
"1502309289": -1,
"1502395763": -1,
"1502482211": -1,
"1502568580": -1,
"1502655044": -1,
"1502741651": -1,
"1502827775": -1,
"1502914685": -1


I previously had more values, and this does not make sense, so I've made a code to limit it to a maximum of 15 values.



            $keys = array_keys((array)$pro["sources"][$id]);
if(count($keys)>15)
{
$keys = asort($keys);
foreach($keys as $key)
{
if(count($keys) <= 15) break;
else
{
unset($pro["sources"][$id][$key]);
unset($keys[$key]);
}
}
}


my question is is there a php function that says "save me the last 15 elements of the arrray, but remove all the others"?










share|improve this question













marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 16:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    array_slice
    – Federico klez Culloca
    Nov 19 at 16:25






  • 4




    If I'm understanding correctly, then array_slice($array, -15)?
    – George
    Nov 19 at 16:25















up vote
1
down vote

favorite













This question already has an answer here:




  • PHP get the last 3 elements of an array

    4 answers




I have a database on couchdb, each document has a slightly complex structure, here is a small part:



   "category": {
"1496805145": -1,
"1497064141": -1,
"1497150993": -1,
"1497322542": -1,
"1497487102": -1,
"1497571701": -1,
"1497657026": -1,
"1497749178": -1,
"1497920895": -1,
"1498005644": -1,
"1498091037": -1,
"1498179039": -1,
"1502183511": -1,
"1502309289": -1,
"1502395763": -1,
"1502482211": -1,
"1502568580": -1,
"1502655044": -1,
"1502741651": -1,
"1502827775": -1,
"1502914685": -1


I previously had more values, and this does not make sense, so I've made a code to limit it to a maximum of 15 values.



            $keys = array_keys((array)$pro["sources"][$id]);
if(count($keys)>15)
{
$keys = asort($keys);
foreach($keys as $key)
{
if(count($keys) <= 15) break;
else
{
unset($pro["sources"][$id][$key]);
unset($keys[$key]);
}
}
}


my question is is there a php function that says "save me the last 15 elements of the arrray, but remove all the others"?










share|improve this question













marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 16:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    array_slice
    – Federico klez Culloca
    Nov 19 at 16:25






  • 4




    If I'm understanding correctly, then array_slice($array, -15)?
    – George
    Nov 19 at 16:25













up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • PHP get the last 3 elements of an array

    4 answers




I have a database on couchdb, each document has a slightly complex structure, here is a small part:



   "category": {
"1496805145": -1,
"1497064141": -1,
"1497150993": -1,
"1497322542": -1,
"1497487102": -1,
"1497571701": -1,
"1497657026": -1,
"1497749178": -1,
"1497920895": -1,
"1498005644": -1,
"1498091037": -1,
"1498179039": -1,
"1502183511": -1,
"1502309289": -1,
"1502395763": -1,
"1502482211": -1,
"1502568580": -1,
"1502655044": -1,
"1502741651": -1,
"1502827775": -1,
"1502914685": -1


I previously had more values, and this does not make sense, so I've made a code to limit it to a maximum of 15 values.



            $keys = array_keys((array)$pro["sources"][$id]);
if(count($keys)>15)
{
$keys = asort($keys);
foreach($keys as $key)
{
if(count($keys) <= 15) break;
else
{
unset($pro["sources"][$id][$key]);
unset($keys[$key]);
}
}
}


my question is is there a php function that says "save me the last 15 elements of the arrray, but remove all the others"?










share|improve this question














This question already has an answer here:




  • PHP get the last 3 elements of an array

    4 answers




I have a database on couchdb, each document has a slightly complex structure, here is a small part:



   "category": {
"1496805145": -1,
"1497064141": -1,
"1497150993": -1,
"1497322542": -1,
"1497487102": -1,
"1497571701": -1,
"1497657026": -1,
"1497749178": -1,
"1497920895": -1,
"1498005644": -1,
"1498091037": -1,
"1498179039": -1,
"1502183511": -1,
"1502309289": -1,
"1502395763": -1,
"1502482211": -1,
"1502568580": -1,
"1502655044": -1,
"1502741651": -1,
"1502827775": -1,
"1502914685": -1


I previously had more values, and this does not make sense, so I've made a code to limit it to a maximum of 15 values.



            $keys = array_keys((array)$pro["sources"][$id]);
if(count($keys)>15)
{
$keys = asort($keys);
foreach($keys as $key)
{
if(count($keys) <= 15) break;
else
{
unset($pro["sources"][$id][$key]);
unset($keys[$key]);
}
}
}


my question is is there a php function that says "save me the last 15 elements of the arrray, but remove all the others"?





This question already has an answer here:




  • PHP get the last 3 elements of an array

    4 answers








php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 16:23









Carlos

416




416




marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 16:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by RiggsFolly php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 16:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3




    array_slice
    – Federico klez Culloca
    Nov 19 at 16:25






  • 4




    If I'm understanding correctly, then array_slice($array, -15)?
    – George
    Nov 19 at 16:25














  • 3




    array_slice
    – Federico klez Culloca
    Nov 19 at 16:25






  • 4




    If I'm understanding correctly, then array_slice($array, -15)?
    – George
    Nov 19 at 16:25








3




3




array_slice
– Federico klez Culloca
Nov 19 at 16:25




array_slice
– Federico klez Culloca
Nov 19 at 16:25




4




4




If I'm understanding correctly, then array_slice($array, -15)?
– George
Nov 19 at 16:25




If I'm understanding correctly, then array_slice($array, -15)?
– George
Nov 19 at 16:25












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Use array_slice($a, -15, 15, true); where $a is the array.



-15 tells the function to start from 15 elements from the end.



15 tells the function to get 15 elements.



true tells the function to also preserve the keys, and I am assuming you do.



If you do not want to preserve the keys, you can use array_slice($a, -15); Since the third argument is missing, it will get the elements until the last is reached (so, 15)






share|improve this answer























  • Yes, but that works without preserving the key. Since false is the default argument.
    – Kristjan Kica
    Nov 19 at 16:29


















up vote
2
down vote













I think you want array_slice(). You give it an offset and a length, and it will return those values as an array. See the PHP docs: http://php.net/manual/en/function.array-slice.php



an example of this:



$truncatedArray = array_slice($pro, -15);


This would give you the last 15 elements of the array $pro






share|improve this answer



















  • 1




    It would be better to include the essential parts of the answer here and provide the link for reference
    – B001ᛦ
    Nov 19 at 16:29










  • @B001ᛦ alright, that makes sense. Thank you.
    – jamie schnaitter
    Nov 19 at 16:33


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Use array_slice($a, -15, 15, true); where $a is the array.



-15 tells the function to start from 15 elements from the end.



15 tells the function to get 15 elements.



true tells the function to also preserve the keys, and I am assuming you do.



If you do not want to preserve the keys, you can use array_slice($a, -15); Since the third argument is missing, it will get the elements until the last is reached (so, 15)






share|improve this answer























  • Yes, but that works without preserving the key. Since false is the default argument.
    – Kristjan Kica
    Nov 19 at 16:29















up vote
2
down vote



accepted










Use array_slice($a, -15, 15, true); where $a is the array.



-15 tells the function to start from 15 elements from the end.



15 tells the function to get 15 elements.



true tells the function to also preserve the keys, and I am assuming you do.



If you do not want to preserve the keys, you can use array_slice($a, -15); Since the third argument is missing, it will get the elements until the last is reached (so, 15)






share|improve this answer























  • Yes, but that works without preserving the key. Since false is the default argument.
    – Kristjan Kica
    Nov 19 at 16:29













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Use array_slice($a, -15, 15, true); where $a is the array.



-15 tells the function to start from 15 elements from the end.



15 tells the function to get 15 elements.



true tells the function to also preserve the keys, and I am assuming you do.



If you do not want to preserve the keys, you can use array_slice($a, -15); Since the third argument is missing, it will get the elements until the last is reached (so, 15)






share|improve this answer














Use array_slice($a, -15, 15, true); where $a is the array.



-15 tells the function to start from 15 elements from the end.



15 tells the function to get 15 elements.



true tells the function to also preserve the keys, and I am assuming you do.



If you do not want to preserve the keys, you can use array_slice($a, -15); Since the third argument is missing, it will get the elements until the last is reached (so, 15)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 11:18

























answered Nov 19 at 16:26









Kristjan Kica

2,0741826




2,0741826












  • Yes, but that works without preserving the key. Since false is the default argument.
    – Kristjan Kica
    Nov 19 at 16:29


















  • Yes, but that works without preserving the key. Since false is the default argument.
    – Kristjan Kica
    Nov 19 at 16:29
















Yes, but that works without preserving the key. Since false is the default argument.
– Kristjan Kica
Nov 19 at 16:29




Yes, but that works without preserving the key. Since false is the default argument.
– Kristjan Kica
Nov 19 at 16:29












up vote
2
down vote













I think you want array_slice(). You give it an offset and a length, and it will return those values as an array. See the PHP docs: http://php.net/manual/en/function.array-slice.php



an example of this:



$truncatedArray = array_slice($pro, -15);


This would give you the last 15 elements of the array $pro






share|improve this answer



















  • 1




    It would be better to include the essential parts of the answer here and provide the link for reference
    – B001ᛦ
    Nov 19 at 16:29










  • @B001ᛦ alright, that makes sense. Thank you.
    – jamie schnaitter
    Nov 19 at 16:33















up vote
2
down vote













I think you want array_slice(). You give it an offset and a length, and it will return those values as an array. See the PHP docs: http://php.net/manual/en/function.array-slice.php



an example of this:



$truncatedArray = array_slice($pro, -15);


This would give you the last 15 elements of the array $pro






share|improve this answer



















  • 1




    It would be better to include the essential parts of the answer here and provide the link for reference
    – B001ᛦ
    Nov 19 at 16:29










  • @B001ᛦ alright, that makes sense. Thank you.
    – jamie schnaitter
    Nov 19 at 16:33













up vote
2
down vote










up vote
2
down vote









I think you want array_slice(). You give it an offset and a length, and it will return those values as an array. See the PHP docs: http://php.net/manual/en/function.array-slice.php



an example of this:



$truncatedArray = array_slice($pro, -15);


This would give you the last 15 elements of the array $pro






share|improve this answer














I think you want array_slice(). You give it an offset and a length, and it will return those values as an array. See the PHP docs: http://php.net/manual/en/function.array-slice.php



an example of this:



$truncatedArray = array_slice($pro, -15);


This would give you the last 15 elements of the array $pro







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 16:36

























answered Nov 19 at 16:25









jamie schnaitter

846




846








  • 1




    It would be better to include the essential parts of the answer here and provide the link for reference
    – B001ᛦ
    Nov 19 at 16:29










  • @B001ᛦ alright, that makes sense. Thank you.
    – jamie schnaitter
    Nov 19 at 16:33














  • 1




    It would be better to include the essential parts of the answer here and provide the link for reference
    – B001ᛦ
    Nov 19 at 16:29










  • @B001ᛦ alright, that makes sense. Thank you.
    – jamie schnaitter
    Nov 19 at 16:33








1




1




It would be better to include the essential parts of the answer here and provide the link for reference
– B001ᛦ
Nov 19 at 16:29




It would be better to include the essential parts of the answer here and provide the link for reference
– B001ᛦ
Nov 19 at 16:29












@B001ᛦ alright, that makes sense. Thank you.
– jamie schnaitter
Nov 19 at 16:33




@B001ᛦ alright, that makes sense. Thank you.
– jamie schnaitter
Nov 19 at 16:33



Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]