Re: getting the "next" element of an associative array

From: Date: Thu, 12 Jul 2007 06:38:10 +0000
Subject: Re: getting the "next" element of an associative array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258669@lists.php.net to get a copy of this message
Olav Mørkrid wrote:
let's say we have the following associative array: $array = array( "red" => "ferrari", "yellow" => "volkswagen", "green" => "mercedes", "blue" => "volvo" ); then we have a current index into the array: $index = "yellow"; $current = $array[$index]; now: how do i get the key of the next array element (in this case "green")? $next = ? Give this a shot
<?php function array_next($ar, $curr) { $capture = false; $next = ''; foreach ( $ar AS $k => $v ) { if ( $capture ) { return array($k => $v); } if ( $k == $curr ) { $capture = true; } } return $next; } $array = array( "red" => "ferrari", "yellow" => "volkswagen", "green" => "mercedes", "blue" => "volvo" ); print_r( array_next($array, 'yellow') );

« previous php.general (#258669) next »