Retrieving array values by range of keys

This is a discussion on Retrieving array values by range of keys within the PHP Language forums, part of the PHP Programming Forums category; Hello there! Suppose I have an associative array that looks like this: $arr['alpha_1'] = 'some value'; $arr['alpha_3'] = 'some value'; $...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-15-2007
WildernessCat
 
Posts: n/a
Default Retrieving array values by range of keys

Hello there!

Suppose I have an associative array that looks like this:
$arr['alpha_1'] = 'some value';
$arr['alpha_3'] = 'some value';
$arr['alpha_4'] = 'some value';
$arr['beta_2'] = 'some value';
$arr['beta_4'] = 'some value';
$arr['beta_6'] = 'some value';
$arr['gamma_1'] = 'some value';
$arr['gamma_5'] = 'some value';
$arr['gamma_6'] = 'some value';
$arr['gamma_7'] = 'some value';

Let's assume that the array is reasonably large (hundreds of entries).

Is there an efficient way of getting all the items whose key starts
with 'beta'? (without doing a foreach/strncmp loop?)

Thanks in advance,
Danny

Reply With Quote
  #2 (permalink)  
Old 10-15-2007
Captain Paralytic
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:
> Hello there!
>
> Suppose I have an associative array that looks like this:
> $arr['alpha_1'] = 'some value';
> $arr['alpha_3'] = 'some value';
> $arr['alpha_4'] = 'some value';
> $arr['beta_2'] = 'some value';
> $arr['beta_4'] = 'some value';
> $arr['beta_6'] = 'some value';
> $arr['gamma_1'] = 'some value';
> $arr['gamma_5'] = 'some value';
> $arr['gamma_6'] = 'some value';
> $arr['gamma_7'] = 'some value';
>
> Let's assume that the array is reasonably large (hundreds of entries).
>
> Is there an efficient way of getting all the items whose key starts
> with 'beta'? (without doing a foreach/strncmp loop?)
>
> Thanks in advance,
> Danny


Something like:

preg_grep('/^beta/',array_keys($arr));

should give you all the keys that start with 'beta'

Reply With Quote
  #3 (permalink)  
Old 10-15-2007
Sanders Kaufman
 
Posts: n/a
Default Re: Retrieving array values by range of keys

"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1192454252.346643.76180@i13g2000prf.googlegro ups.com...
> On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:
>> Hello there!
>>
>> Suppose I have an associative array that looks like this:
>> $arr['alpha_1'] = 'some value';
>> $arr['alpha_3'] = 'some value';
>> $arr['alpha_4'] = 'some value';
>> $arr['beta_2'] = 'some value';
>> $arr['beta_4'] = 'some value';
>> $arr['beta_6'] = 'some value';
>> $arr['gamma_1'] = 'some value';
>> $arr['gamma_5'] = 'some value';
>> $arr['gamma_6'] = 'some value';
>> $arr['gamma_7'] = 'some value';
>>
>> Let's assume that the array is reasonably large (hundreds of entries).
>>
>> Is there an efficient way of getting all the items whose key starts
>> with 'beta'? (without doing a foreach/strncmp loop?)
>>
>> Thanks in advance,
>> Danny

>
> Something like:
>
> preg_grep('/^beta/',array_keys($arr));
>
> should give you all the keys that start with 'beta'


What does the return value look like - array? string?


Reply With Quote
  #4 (permalink)  
Old 10-15-2007
WildernessCat
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:
>
>
>
> > Hello there!

>
> > Suppose I have an associative array that looks like this:
> > $arr['alpha_1'] = 'some value';
> > $arr['alpha_3'] = 'some value';
> > $arr['alpha_4'] = 'some value';
> > $arr['beta_2'] = 'some value';
> > $arr['beta_4'] = 'some value';
> > $arr['beta_6'] = 'some value';
> > $arr['gamma_1'] = 'some value';
> > $arr['gamma_5'] = 'some value';
> > $arr['gamma_6'] = 'some value';
> > $arr['gamma_7'] = 'some value';

>
> > Let's assume that the array is reasonably large (hundreds of entries).

>
> > Is there an efficient way of getting all the items whose key starts
> > with 'beta'? (without doing a foreach/strncmp loop?)

>
> > Thanks in advance,
> > Danny

>
> Something like:
>
> preg_grep('/^beta/',array_keys($arr));
>
> should give you all the keys that start with 'beta'


Yes, it sure looks more compact in the code, but the question is
whether it's more efficient? I have a gut feeling that there is no
efficient way.

By the way I forgot to mention that the array is not sorted.

Reply With Quote
  #5 (permalink)  
Old 10-15-2007
Captain Paralytic
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On 15 Oct, 14:37, "Sanders Kaufman" <bu...@kaufman.net> wrote:
> "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
>
> news:1192454252.346643.76180@i13g2000prf.googlegro ups.com...
>
>
>
>
>
> > On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:
> >> Hello there!

>
> >> Suppose I have an associative array that looks like this:
> >> $arr['alpha_1'] = 'some value';
> >> $arr['alpha_3'] = 'some value';
> >> $arr['alpha_4'] = 'some value';
> >> $arr['beta_2'] = 'some value';
> >> $arr['beta_4'] = 'some value';
> >> $arr['beta_6'] = 'some value';
> >> $arr['gamma_1'] = 'some value';
> >> $arr['gamma_5'] = 'some value';
> >> $arr['gamma_6'] = 'some value';
> >> $arr['gamma_7'] = 'some value';

>
> >> Let's assume that the array is reasonably large (hundreds of entries).

>
> >> Is there an efficient way of getting all the items whose key starts
> >> with 'beta'? (without doing a foreach/strncmp loop?)

>
> >> Thanks in advance,
> >> Danny

>
> > Something like:

>
> > preg_grep('/^beta/',array_keys($arr));

>
> > should give you all the keys that start with 'beta'

>
> What does the return value look like - array? string?- Hide quoted text -
>
> - Show quoted text -


Why ask me when you can just go to:
http://uk3.php.net/manual/en/function.preg-grep.php
and read it there?

Note that this won't solve the OP's whole problem as he wants the
values rather than the keys, but it's a start.

Reply With Quote
  #6 (permalink)  
Old 10-15-2007
Sanders Kaufman
 
Posts: n/a
Default Re: Retrieving array values by range of keys

"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1192455950.972135.168750@i38g2000prf.googlegr oups.com...
> On 15 Oct, 14:37, "Sanders Kaufman" <bu...@kaufman.net> wrote:


>> > preg_grep('/^beta/',array_keys($arr));

>>
>> > should give you all the keys that start with 'beta'

>>
>> What does the return value look like - array? string?- Hide quoted
>> text -
>>
>> - Show quoted text -

>
> Why ask me when you can just go to:
> http://uk3.php.net/manual/en/function.preg-grep.php
> and read it there?


Because, after a bout with meningitis, my brain doesn't work as well as it
used to.
I read one page after another - and have no idea what I read.
But when I discuss - it registers.


Reply With Quote
  #7 (permalink)  
Old 10-15-2007
Captain Paralytic
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.com> wrote:
> On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
>
>
>
>
> > On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:

>
> > > Hello there!

>
> > > Suppose I have an associative array that looks like this:
> > > $arr['alpha_1'] = 'some value';
> > > $arr['alpha_3'] = 'some value';
> > > $arr['alpha_4'] = 'some value';
> > > $arr['beta_2'] = 'some value';
> > > $arr['beta_4'] = 'some value';
> > > $arr['beta_6'] = 'some value';
> > > $arr['gamma_1'] = 'some value';
> > > $arr['gamma_5'] = 'some value';
> > > $arr['gamma_6'] = 'some value';
> > > $arr['gamma_7'] = 'some value';

>
> > > Let's assume that the array is reasonably large (hundreds of entries).

>
> > > Is there an efficient way of getting all the items whose key starts
> > > with 'beta'? (without doing a foreach/strncmp loop?)

>
> > > Thanks in advance,
> > > Danny

>
> > Something like:

>
> > preg_grep('/^beta/',array_keys($arr));

>
> > should give you all the keys that start with 'beta'

>
> Yes, it sure looks more compact in the code, but the question is
> whether it's more efficient? I have a gut feeling that there is no
> efficient way.
>
> By the way I forgot to mention that the array is not sorted.- Hide quoted text -
>
> - Show quoted text -


The fact that it is not sorted shouldn't matter. As far as efficiency
goes, it is all handled by a BIF as opposed to interpreted code, so it
has the opportunity to be more efficient.

Now, I don't know how you want to use the results but this leaves you
with an array containing all the keys that you need for later use.

Reply With Quote
  #8 (permalink)  
Old 10-16-2007
WildernessCat
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On Oct 15, 5:05 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.com> wrote:
>
>
>
> > On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:

>
> > > On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:

>
> > > > Hello there!

>
> > > > Suppose I have an associative array that looks like this:
> > > > $arr['alpha_1'] = 'some value';
> > > > $arr['alpha_3'] = 'some value';
> > > > $arr['alpha_4'] = 'some value';
> > > > $arr['beta_2'] = 'some value';
> > > > $arr['beta_4'] = 'some value';
> > > > $arr['beta_6'] = 'some value';
> > > > $arr['gamma_1'] = 'some value';
> > > > $arr['gamma_5'] = 'some value';
> > > > $arr['gamma_6'] = 'some value';
> > > > $arr['gamma_7'] = 'some value';

>
> > > > Let's assume that the array is reasonably large (hundreds of entries).

>
> > > > Is there an efficient way of getting all the items whose key starts
> > > > with 'beta'? (without doing a foreach/strncmp loop?)

>
> > > > Thanks in advance,
> > > > Danny

>
> > > Something like:

>
> > > preg_grep('/^beta/',array_keys($arr));

>
> > > should give you all the keys that start with 'beta'

>
> > Yes, it sure looks more compact in the code, but the question is
> > whether it's more efficient? I have a gut feeling that there is no
> > efficient way.

>
> > By the way I forgot to mention that the array is not sorted.- Hide quoted text -

>
> > - Show quoted text -

>
> The fact that it is not sorted shouldn't matter. As far as efficiency
> goes, it is all handled by a BIF as opposed to interpreted code, so it
> has the opportunity to be more efficient.
>
> Now, I don't know how you want to use the results but this leaves you
> with an array containing all the keys that you need for later use.


Thanks. I think I'll go with this one.

Reply With Quote
  #9 (permalink)  
Old 10-16-2007
WildernessCat
 
Posts: n/a
Default Re: Retrieving array values by range of keys

On Oct 15, 5:05 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.com> wrote:
>
>
>
> > On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:

>
> > > On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.com> wrote:

>
> > > > Hello there!

>
> > > > Suppose I have an associative array that looks like this:
> > > > $arr['alpha_1'] = 'some value';
> > > > $arr['alpha_3'] = 'some value';
> > > > $arr['alpha_4'] = 'some value';
> > > > $arr['beta_2'] = 'some value';
> > > > $arr['beta_4'] = 'some value';
> > > > $arr['beta_6'] = 'some value';
> > > > $arr['gamma_1'] = 'some value';
> > > > $arr['gamma_5'] = 'some value';
> > > > $arr['gamma_6'] = 'some value';
> > > > $arr['gamma_7'] = 'some value';

>
> > > > Let's assume that the array is reasonably large (hundreds of entries).

>
> > > > Is there an efficient way of getting all the items whose key starts
> > > > with 'beta'? (without doing a foreach/strncmp loop?)

>
> > > > Thanks in advance,
> > > > Danny

>
> > > Something like:

>
> > > preg_grep('/^beta/',array_keys($arr));

>
> > > should give you all the keys that start with 'beta'

>
> > Yes, it sure looks more compact in the code, but the question is
> > whether it's more efficient? I have a gut feeling that there is no
> > efficient way.

>
> > By the way I forgot to mention that the array is not sorted.- Hide quoted text -

>
> > - Show quoted text -

>
> The fact that it is not sorted shouldn't matter. As far as efficiency
> goes, it is all handled by a BIF as opposed to interpreted code, so it
> has the opportunity to be more efficient.
>
> Now, I don't know how you want to use the results but this leaves you
> with an array containing all the keys that you need for later use.


Thanks! I think I'll go with this option.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:28 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0