Something like reset() or end() except . . .

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dckx
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 8:15 pm

Something like reset() or end() except . . .

Post by dckx »

Suppose I have the following array:

100 => "Orange"
200 => "Red"
10 => "Blue"
50 => "Green"
150 => "Yellow"

Given the key 10, how do I find keys 200 and 50? Or given the key 150, how do I find keys 50 and 100?

If I knew key(current()) would return 10 I'd just use prev() and next(), but I have no guaranty where current() is.

I know I could use keys() and search, but that won't scale and it is just so inelegant. I need something like reset() or end() except it sets the internal pointer to a specified element. This is PHP 5.2.5.

Thanks for the help.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Something like reset() or end() except . . .

Post by Christopher »

current(), prev() and next() are for sequential access. I think I would get array_keys() and use that array to traverse the data. It has sequence information.
(#10850)
dckx
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 8:15 pm

Re: Something like reset() or end() except . . .

Post by dckx »

arborint wrote:I think I would get array_keys() and use that array to traverse the data.
I'd like to avoid a method like this. It isn't scalable and its non-deterministic.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Something like reset() or end() except . . .

Post by Christopher »

dckx wrote:I'd like to avoid a method like this. It isn't scalable and its non-deterministic.
First, I don't see how it is not scalable or non-deterministic. The problem with arrays will be memory usage, not hash lookups. Not sure how it could be any more "deterministic" that you have a lookup array into another array. It's 1:1.

Second, I don't think you really have many other options of you want to use arrays and do it in memory.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Something like reset() or end() except . . .

Post by Chris Corbyn »

Code: Select all

<?php
 
$array = array(
  100 => 'Red',
  50 => 'Green',
  150 => 'Blue',
  10 => 'Yellow',
  25 => 'Indigoo',
  75 => 'Violet'
);
 
$keys = array_keys($array);
 
$mid = 150;
 
while ($key = next($keys)) {
  if ($mid == $key) {
    $subset = array_slice($keys, key($keys) - 1, 3);
    printf('Before = %s, After = %s', current($subset), end($subset));
  }
}
Prints: Before = 50, After = 10.
dckx
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 8:15 pm

Re: Something like reset() or end() except . . .

Post by dckx »

In other languages it is possible to get the 'real' index of an element given its key. And it is possible to access elements with the 'real' index. You don't need a second array at all. It's scalable and deterministic as you aren't making second copies of anything and you aren't doing linear searches on anything.

I'm a little surprised not to find this as PHP seems to have a pretty and efficient interface to just about everything I've needed to do.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Something like reset() or end() except . . .

Post by Christopher »

dckx wrote:In other languages it is possible to get the 'real' index of an element given its key. And it is possible to access elements with the 'real' index. You don't need a second array at all. It's scalable and deterministic as you aren't making second copies of anything and you aren't doing linear searches on anything.

I'm a little surprised not to find this as PHP seems to have a pretty and efficient interface to just about everything I've needed to do.
By 'real' index I assume you mean a memory pointer. There are a few languages that allow you to do this, but most don't. Not quite sure what to tell you. There are several way to do what you want, but you don't seem to like them.
(#10850)
dckx
Forum Newbie
Posts: 4
Joined: Fri Apr 11, 2008 8:15 pm

Re: Something like reset() or end() except . . .

Post by dckx »

Thanks for the help. Mainly I didn't want to go about it one way and then learn PHP had a built-in that did what I needed. Imagine how simple it'd all be if there just was: set_current(array, key);

Also (am I just saying this because 1: I said I wanted scalability/determinism and 2: both suggested solutions do it) you can achieve this by making a second array or by doing the linear search. Their is no need to do both.
Post Reply