Reply to Re: [PHP] How do I start at a specific position in an array? Is there

Your name:

Reply:


Posted by Robert Cummings on 10/05/98 11:05

On Mon, 2005-01-10 at 11:28, Richard Lynch wrote:
> Thomas Goyne wrote:
> > On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz <tularis@php.net> wrote:
> >
> >> that's not a SPECIFIC place in the array, that's just current, next and
> >> previous. AFAIK there is no way to explicitly set the internal pointer
> >> of the array to a spcified place. I used a function which basically
> >> looped trough the array until it got to the correct depth, and then
> >> returned it by reference....but it's not very efficient :S
> >>
> >
> > Why would you ever want to do that other than to waste cycles?
>
> I'll give you a simple case.
>
> I have a GTK PHP MP3 ID3 editor application I'm working on.
>
> When one opens a file in a directory, I provide next/prev buttons to
> quickly page to the next/prev file in the directory.
>
> Getting the next/prev to work is simple enough, but...
>
> I've got the whole array built from opendir/readdir, and sorted it into
> alphabetical order.
>
> I've got a filename inside that array, from the pre-defined
> GtkFileSelection dialog.
>
> So I need to initialize the internal array pointer to the position of the
> file within that array, there is no such PHP function.

Why not change your structure? Sounds like you want a linked list,
better yet a doubly linked list. So create a double linked list class,
link up your entries, and keep a hash available for fast lookup into the
linked list where the keys are the filenames or whatever you want to use
as an index, and the value is a reference to the interesting linked list
node.

Given that scenario you will have a currNode object which is the
currently selected song, and when someone moves up or down the list you
just do something like:

if( $currNode->hasNext() )
{
$currNode = &$currNode->next();
}

Moving backward would be as simple as:

if( $currNode->hasPrevious() )
{
$currNode = &$currNode->previous();
}

And if the user selects an arbitrary file:

$currNode = &$lookupArray[$index];

This kind of structure works a lot like a doubly threaded red-black tree
:) Although a red-black tree would keep your entries in sorted order
whenever you add new entries in O( lg n ) time.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация