PHP Associative Array to slice or get first n elements and preserve keys

From an array, want to get first 5 elements with keys preserved. The array_slice() with 2nd and 3rd parameter will limit the array to get first 5 elements.

$array = (
    "1" => "News",
    "14" => "Videos",
    "275" => "Time",
    "24" => "Stories",
    "33" => "Pictures",
    "422" => "Pranks",
    "889" => "People",
    "1978" => "Tweets"
);
Fourth parameter set to be TRUE to preserve keys.

Syntax
$array = array_slice($array, 0, 5, TRUE);
Output
$array = (
    "1" => "News",
    "14" => "Videos",
    "275" => "Time",
    "24" => "Stories",
    "33" => "Pictures"
);
 
PHP Array Array Slice Posted on 2016-04-17 05:44:28