473,513 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Oct 15 '07 #1
8 3048
On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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'

Oct 15 '07 #2
"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:11*********************@i13g2000prf.googlegro ups.com...
On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
>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?
Oct 15 '07 #3
On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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.

Oct 15 '07 #4
On 15 Oct, 14:37, "Sanders Kaufman" <bu...@kaufman.netwrote:
"Captain Paralytic" <paul_laut...@yahoo.comwrote in message

news:11*********************@i13g2000prf.googlegro ups.com...


On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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.

Oct 15 '07 #5
"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On 15 Oct, 14:37, "Sanders Kaufman" <bu...@kaufman.netwrote:
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.
Oct 15 '07 #6
On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.comwrote:
On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:


On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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.

Oct 15 '07 #7
On Oct 15, 5:05 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.comwrote:
On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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.

Oct 16 '07 #8
On Oct 15, 5:05 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 15 Oct, 14:43, WildernessCat <wilderness...@gmail.comwrote:
On Oct 15, 3:17 pm, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 15 Oct, 14:09, WildernessCat <wilderness...@gmail.comwrote:
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.

Oct 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3502
by: Andy Levy | last post by:
Hi I have a form which contains several fields. One of which is a group of checkboxes that can all be selected, and if they are - all values should be passed as an array. Each checkbox is...
22
2105
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
14
3437
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
5
5790
by: AP | last post by:
Hello Any suggestions on the most efficient way to generate a random number from a range that will not return values specified in an array?
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.