php - How to compress an array? -
lets say, have array 1000 values (integers). , need array have array f.e. 400 values (the number can changed, f.e. 150, etc.).
so need return each 2.5th array value, i.e. 1st, 3rd, 6th, 8th, 11th, etc.
is somehow possible?
i dont need code, need way, how it.
edited:
my array array of elevations (from array gps coordinates). , want draw 2d model of elevation. lets say, map have 400px
width. 1px
= 1 point of elevation. thats why need each 2.5th elevation value...
like this:
you don't want 2.5th one. want divide set blocks of 5 , first , third each set.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
you want first , third columns.
this php, we've got 0-based arrays.
we can divide groups of 5 using modulo operator %
. can see if return value 0
(i.e. it's in first column) or 2
(i.e. it's in third column).
i'm going presume array has numeric keys starting 0
.
// php 5.6 $filtered = array_filter($array, function($value, $key) { $mod = $key % 5; return ($mod === 0) || ($mod === 2); }, array_filter_use_both); // pre-php 5.6 $filtered = array(); foreach ($array $key => $value) { $mod = $key % 5; if (($mod === 0) || ($mod === 2)) { $filtered[$key] = $value; } } var_dump($filtered);
Comments
Post a Comment