Referencing multiple keys in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jack Hare
Forum Newbie
Posts: 2
Joined: Tue Feb 02, 2010 1:22 am

Referencing multiple keys in an array

Post by Jack Hare »

I can't shake the feeling that there is something very basic I'm missing, but I've been trying various search phrasings and reading about arrays on different sites for several hours now and either haven't found the answer, or haven't understood well enough to recognize it.

I want to assign $p_type based on whether or not $tag matches any of keys 1 through 5 (that is, the second through sixth keys out of a longer list) in the associative array $queen_index. Is there a way to do this using something in place of the asterisked phrase below, or something of similar simplicity? Or is there really no way other than with a longer code structure such as a while or foreach?

Code: Select all

if ($tag == $queen_index [/*1 through 5*/]) { $p_type = "section"; }
    else { $p_type = "subpage"; }
I have ideas for various workarounds and have received suggestions for others, but all of them seem 'inelegant' -- adding further lines of code while requiring $p_type, $tag, and/or $queen_index to be reworked, along with substantial other sections dependent on them (I can provide further code for context if necessary, but for the moment explaining how these three items relate to the rest of the code would probably just muddy the waters needlessly). If that is necessary, then so be it, but if it can be avoided I'd like to.

The heart of my question therefore is, am I overlooking a basic way to state several specific keys from an array at once? It seems odd that there should be none.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Referencing multiple keys in an array

Post by VladSun »

Code: Select all

if (array_key_exists(array_slice($queen_index, 0, 5), $tag))
...
Though, I think you should review your code ... If you need things like these, something is really wrong.
There are 10 types of people in this world, those who understand binary and those who don't
Jack Hare
Forum Newbie
Posts: 2
Joined: Tue Feb 02, 2010 1:22 am

Re: Referencing multiple keys in an array

Post by Jack Hare »

VladSun wrote:

Code: Select all

if (array_key_exists(array_slice($queen_index, 0, 5), $tag))
...
Though, I think you should review your code ... If you need things like these, something is really wrong.
That seems like a marvelous solution, and I should have been able to derive it myself, but your comment has me worried again. What do you think I'm doing wrong?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Referencing multiple keys in an array

Post by VladSun »

Maybe if you post more of your code (no HTML) regarding this issue we will be more helpful.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply