thepip3r Posted January 18, 2010 Share Posted January 18, 2010 I'm probably just rusty and can't recall but is there a way to mathematically get the last 20 values of a dynamically sized associative array? I realize I can remap the array with numeric indexes and then use that, I just wanted to make sure I'm not missing the easier (more efficient) way. Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/ Share on other sites More sharing options...
wildteen88 Posted January 18, 2010 Share Posted January 18, 2010 You could use array_slice Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-997558 Share on other sites More sharing options...
thepip3r Posted January 18, 2010 Author Share Posted January 18, 2010 so array_slice() into a new array and count off of it's values correct? Ty wildteen. Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-997562 Share on other sites More sharing options...
wildteen88 Posted January 18, 2010 Share Posted January 18, 2010 Yes you would use array_slice() like so $last20 = array_slice($array, -20, 20, true); $last20 will store a copy of the last 20 items in your array ($array); Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-997563 Share on other sites More sharing options...
thepip3r Posted January 18, 2010 Author Share Posted January 18, 2010 ah even better... i was thinking about using it to create an entirely new indexed array and then using a for() loop to loop through... tyvm for the clarification. Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-997576 Share on other sites More sharing options...
nrg_alpha Posted January 19, 2010 Share Posted January 19, 2010 With the use of array_slice, the last 20 entries will be retained (including their indices.. so using an array with say 25 indices, the new array will start at index 5 and go through to 24: $arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); $last20 = array_slice($arr, -20, 20, true); echo '<pre>'.print_r($last20, true); // index 5 thru 24, with values 6 thru 25 Not sure if this is a big deal or not.. but if you want to have the indices renamed from from 0 instead, you can use array_splice instead: $arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); $last20 = array_splice($arr, -20); echo '<pre>'.print_r($last20, true); // index 0 thru 19, with values from 6 thru 25 Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-997775 Share on other sites More sharing options...
thepip3r Posted January 19, 2010 Author Share Posted January 19, 2010 nrg... even better... tyvm for the comment Link to comment https://forums.phpfreaks.com/topic/188929-advicephp-internal-function-get-last-20-associative-array-values/#findComment-998168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.