note 40103 deleted from function.array-slice by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 12:31:15 +0000 |
| Subject: | note 40103 deleted from function.array-slice by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213707@lists.php.net to get a copy of this message | ||
Note Submitter: jenny at jennys dot info
----
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
// )
?>