note 101964 added to function.array-slice

From: Date: Thu, 20 Jan 2011 13:22:40 +0000
Subject: note 101964 added to function.array-slice
Groups: php.notes 
Request: Send a blank email to php-notes+get-175931@lists.php.net to get a copy of this message
Here's an array_slice function for associative arrays. It slices by array key from and including that key. If the $length is a string it is assumed to be another array key and the array is sliced up to but not including the end key otherwise it slices that length. <?php function array_slice_assoc ($array, $key, $length, $preserve_keys = true) { $offset = array_search($key, array_keys($array)); if (is_string($length)) $length = array_search($length, array_keys($array)) - $offset; return array_slice($array, $offset, $length, $preserve_keys); } ?> Example usage: <?php $arr = array('a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e', 'f' => 'f'); print_r(array_slice_assoc($arr, 'b', 2, true)); /* Array ( [b] => b [c] => c ) */ print_r(array_slice_assoc($arr, 'b', 'e', true)); /* Array ( [b] => b [c] => c [d] => d ) */ print_r(array_slice_assoc($arr, 'b', -1, true)); /* Array ( [b] => b [c] => c [d] => d [e] => e ) */ ?> ---- Server IP: 92.48.74.199 Probable Submitter: 86.138.225.15 ---- Manual Page -- http://www.php.net/manual/en/function.array-slice.php Edit -- https://master.php.net/note/edit/101964 Del: integrated -- https://master.php.net/note/delete/101964/integrated Del: useless -- https://master.php.net/note/delete/101964/useless Del: bad code -- https://master.php.net/note/delete/101964/bad+code Del: spam -- https://master.php.net/note/delete/101964/spam Del: non-english -- https://master.php.net/note/delete/101964/non-english Del: in docs -- https://master.php.net/note/delete/101964/in+docs Del: other reasons-- https://master.php.net/note/delete/101964 Reject -- https://master.php.net/note/reject/101964 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175931) next »