note 40103 added to function.array-slice

From: Date: Sun, 22 Feb 2004 03:12:32 +0000
Subject: note 40103 added to function.array-slice
Groups: php.notes 
Request: Send a blank email to php-notes+get-65505@lists.php.net to get a copy of this message
Here's a function which returns the array offset based on the array key. This is useful if you'd like to use array_slice to get all keys/values after key "foo". <? function array_offset($array, $offset_key) { $offset = 0; foreach($array as $key=>$val) { if($key == $offset_key) return $offset; $offset++; } return -1; } $array = array('foo'=>'foo', 'bar'=>'bar', 'bash'=>'bash', 'quux'=>'quux'); print_r($array); // Prints the following: // Array // ( // [foo] => foo // [bar] => bar // [bash] => bash // [quux] => quux // ) $offset = array_offset($array,'bar'); // $offset now contains '1' $new = array_slice($array,$offset+1); print_r($new); // Prints the following: // Array // ( // [bash] => bash // [quux] => quux // ) ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-slice.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40103 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40103&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40103&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65505) next »