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.
Something like reset() or end() except . . .
Moderator: General Moderators
- 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 . . .
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)
Re: Something like reset() or end() except . . .
I'd like to avoid a method like this. It isn't scalable and its non-deterministic.arborint wrote:I think I would get array_keys() and use that array to traverse the data.
- 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 . . .
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.dckx wrote:I'd like to avoid a method like this. It isn't scalable and its non-deterministic.
Second, I don't think you really have many other options of you want to use arrays and do it in memory.
(#10850)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Something like reset() or end() except . . .
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));
}
}Re: Something like reset() or end() except . . .
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.
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.
- 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 . . .
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.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.
(#10850)
Re: Something like reset() or end() except . . .
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.
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.