Getting the previous and next array key?
Hello,
I have an array like this:
('car' => 'Ford',
'computer' => 'Dell',
'deodorant' => 'Rexona',
'television' => 'Sony',
......
)
Assuming that I'm given the key $key='deodorant', what is the easiest way to get the previous key ('computer') and the next key ('television') of the array?
I know I can do it the hard way, but there must be an easy way to do it, maybe in 1 or 2 lines of coding?
I don't think that this will work. next() and prev() works with current(), but how do I make the key that I have the "current" key?
take the example on php.net
<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = next($transport); // $mode = 'car';
$mode = prev($transport); // $mode = 'bike';
$mode = end($transport); // $mode = 'plane';
?>
now take your array
$thing = array('car' => 'Ford', 'computer' => 'Dell', 'deodorant' => 'Rexona', 'television' => 'Sony');
$item = current($thing); // $item = 'car';
$item = next($thing); // $item = 'computer';
$item = next($thing); // $item = 'deodarant';
//it now knows $item is deodarant
$item = prev($thing); // $item = 'computer';
echo "Previous: ".$item."<br>";
$item = next($thing); // $item = 'deodarant';
echo "Current: ".$item."<br>";
$item = next($thing); // $item = 'television';
echo "Next: ".$item;
//Will Produce
//Previous: Computer
//Current: Deodarant
//Next: Television
Originally posted by 96turnerri
take the example on php.net
<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = next($transport); // $mode = 'car';
$mode = prev($transport); // $mode = 'bike';
$mode = end($transport); // $mode = 'plane';
?>
now take your array
$thing = array('car' => 'Ford', 'computer' => 'Dell', 'deodorant' => 'Rexona', 'television' => 'Sony');
$item = current($thing); // $item = 'car';
$item = next($thing); // $item = 'computer';
$item = next($thing); // $item = 'deodarant';
//it now knows $item is deodarant
$item = prev($thing); // $item = 'computer';
echo "Previous: ".$item."<br>";
$item = next($thing); // $item = 'deodarant';
echo "Current: ".$item."<br>";
$item = next($thing); // $item = 'television';
echo "Next: ".$item;
//Will Produce
//Previous: Computer
//Current: Deodarant
//Next: Television
I still think that this isn't an easy way.
Suppose that the key is parsed, then you only have the variable $key. You will have to cycle through the array to know where it's position is in the array. Isn't there an easy way?
you could use a combination of next, prev and array_search that would do it, please explain in more detail what you are trying to do as im not sure we are talking about the same thing
Rich
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks