Getting the first index of an associative 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Getting the first index of an associative array

Post by superdezign »

Code: Select all

if(!empty($_GET))
{
    foreach($_GET as $get) break;
    if($get == $foo)
   {
        ....
   }
}
I am making links like http://www.domain.tld/page.php?foo instead of the traditional ?foo=bar query string, but I don't want to give any particular action priority over another in case someone were to (strangely) enter more than one into the query string. I'd want it to, expectedly, take the first one and display according to it. The foreach is the only way that I know of to get an associative array in order.

Is there another way to do this (a function perhaps?), or should I just be content with this method?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe

Code: Select all

<?php
$a = array('a'=>null, 'b'=>null);
reset($a);
list($k,$v) = each($a);
echo $k;
?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

if(is_array($_GET)) {
reset($_GET);
echo current($_GET);
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It's true... There are array functions for everything. :-D

I've just realized that if for some reason (one that's actually more possible) there is a get variable before one of the one's I am after, then I'd completely miss it. So, now I'm doing it this way:

Code: Select all

foreach($_GET as $get) if(in_array($get, $validChoices)) break;
After making that change, it seems as though I may as well just stick with the foreach.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

I wonder how your foreach will work if in the GET there is name=value pair. Maybe the $get will be an array and in_array will throw error.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$result = array_unshift(array_flip(array('key1' => 'foo', 'key2' => 'bar')));
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:

Code: Select all

$result = array_unshift(array_flip(array('key1' => 'foo', 'key2' => 'bar')));
I was trying to remember why I hadn't actually wrote the code yet. That was the problem. :-p
Thanks. ^_^
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

There are too much array functions and if you ask 100 coders to do specific array manipulation they all will provide different solutions :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

miro_igov wrote:There are too much array functions and if you ask 100 coders to do specific array manipulation they all will provide different solutions :)
True. array_flip was all I was after. I couldn't remember the function name. ;_;

I'm not going to need array_unshift, though. in_array can take any value.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Code: Select all

$ary = array('foo'=>1, 'bar'=>2);
echo key($ary); // foo
hth ;)
Post Reply