|
Posted by Captain Paralytic on 10/15/07 15:05
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.
Navigation:
[Reply to this message]
|