Page 1 of 1
Something like reset() or end() except . . .
Posted: Fri Apr 11, 2008 8:36 pm
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.
Re: Something like reset() or end() except . . .
Posted: Fri Apr 11, 2008 11:11 pm
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 2:37 am
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 2:43 am
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 2:58 am
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 4:04 am
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 11:24 am
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.
Re: Something like reset() or end() except . . .
Posted: Sat Apr 12, 2008 12:06 pm
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.