PHP extract part from array keeping previous keys PHP extract part from array keeping previous keys php php

PHP extract part from array keeping previous keys


Try array_slice with $preserve_keys set to true.


With array_slice, the original array is not modified:

$levels = array('a' => 1, 'b' =>2, 'c' => 3, 'd' => 4, 'f' => 5, 'g' => 6);$firstLevels = array_slice($levels, 0, 5, true);// count($levels) is 6, count($firstLevels) 5