开发者

Which way efficient to cut from associated array first + last elemets(key + value)

开发者 https://www.devze.com 2023-02-13 17:29 出处:网络
Which way efficient to cut fromassociated array first + last elemets(key + value). (Can be use just unset, but array_pop/array_shift/array_slice not working on associated array)

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消