Which way efficient to cut from associated array first + last elemets(key + value).
(Can be use just unset, but array_pop/array_shift/array_slice not working on associated array)
Example:
input array:
$input=array(20=>'v1', 56=>'v2', 80=>'v3',88=>'v4');
output array:
$input=ar开发者_运维技巧ray( 56=>'v2', 80=>'v3');
Thanks
array_slice()
is the way to go:
$input = array(20=>'v1', 56=>'v2', 80=>'v3',88=>'v4');
$output = array_slice($input, 1, -1, true);
print_r($output);
Output:
Array
(
[56] => v2
[80] => v3
)
Don't forget to specify true
as 4th argument, otherwise keys won't be preserved (i.e., you'll get 0,1,2,...
as keys).
$a = array_slice($a, 1, -1, true);
(Yes this works on associative arrays.)
精彩评论