By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Stupid question but How do I grab the first key of a multidimensionalarray.

P: n/a
SM
Hello,
I've done some research on how to grab the first key of a
multidimensional array. I've came up with a couple of solutions. Wich
is the best?. If possible, can it be done in just one line of code
(see example)?

Here's my sample multidimensional array:
$docs = array(
0=array(
y5Ub55UT=>'doc1',
cI8roXDw=>'doc2'
),
5=array(
HTMFQvUY=>'doc45',
x84vtG2e=>'doc28946'
)
);

How to grab the first element of $docs[5]? If possible in one line of
code...

Here's what i've found:

//this one looks ok to me
$keys = array_keys($docs[5]);
echo $keys[0];
// works but i don't trust this one for some reason
echo array_shift(array_keys($docs[5]));
//works but returns the value NOT the key... to bad
echo reset($docs[5]);

Any ideas on how to grab the first key of a multidimensional array in
one line of code?

Thanks
Marco
Oct 3 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
SM wrote:
//works but returns the value NOT the key... to bad
echo reset($docs[5]);
Pair that with key(). See http://php.net/key

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Now listening to: Dot Allison - Afterglow (1999) - [4] Message Personnel
(6:25) (0.000000%)
Oct 3 '08 #2

P: n/a
SM
On Oct 3, 2:38*pm, Ivn Snchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
SM wrote:
//works but returns the value NOT the key... to bad
echo reset($docs[5]);

Pair that with key(). Seehttp://php.net/key

--
----------------------------------
Ivn Snchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Now listening to: Dot Allison - Afterglow (1999) - [4] Message Personnel
(6:25) (0.000000%)
Who would have thought!
I've tried it even without paring with reset and it works!
Here's the code in one line!!

echo key($docs[5]);

For some reason, it always returns the first key of an array without
the need to use the reset function.

Thanks, i'll go for this one.
Oct 3 '08 #3

P: n/a
..oO(SM)
>Who would have thought!
I've tried it even without paring with reset and it works!
Here's the code in one line!!

echo key($docs[5]);

For some reason, it always returns the first key of an array without
the need to use the reset function.
Only if the internal array pointer points to the first element. You
shouldn't rely on that and explicitly reset the array. If you want a
one-liner, write your own little helper function.

Micha
Oct 3 '08 #4

P: n/a
Michael Fesser wrote:
Only if the internal array pointer points to the first element. You
shouldn't rely on that and explicitly reset the array. If you want a
one-liner, write your own little helper function.
A helper function for just two sentences?

reset($array); echo key($array);

There you have, a one-liner. Maybe you want to use amore esoteric form,
depending on how you would need to catch errors:

reset($array) && echo key($array);

echo reset($array) ? key($array) : null;
Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-3 generating this signature.
Uptime: 22:58:40 up 43 days, 10:54, 4 users, load average: 1.14, 0.87,
0.62

Oct 3 '08 #5

P: n/a
..oO(Ivn Snchez Ortega)
>Michael Fesser wrote:
>Only if the internal array pointer points to the first element. You
shouldn't rely on that and explicitly reset the array. If you want a
one-liner, write your own little helper function.

A helper function for just two sentences?

reset($array); echo key($array);
Maybe he wants to directly use the first element in some other context:

doSomething(getFirstElement($array));

Of course you could also write it as

reset($array);
doSomething(key($array));

But sometimes a little helper is more convenient.

Micha
Oct 3 '08 #6

P: n/a
SM
On Oct 3, 5:01*pm, Ivn Snchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
Michael Fesser wrote:
Only if the internal array pointer points to the first element. You
shouldn't rely on that and explicitly reset the array. If you want a
one-liner, write your own little helper function.

A helper function for just two sentences?

reset($array); echo key($array);

There you have, a one-liner. Maybe you want to use amore esoteric form,
depending on how you would need to catch errors:

reset($array) && echo key($array);

echo reset($array) ? key($array) : null;

Cheers,
--
----------------------------------
Ivn Snchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-3 generating this signature.
Uptime: 22:58:40 up 43 days, 10:54, *4 users, *load average: 1.14, 0.87,
0.62
Thanks again. I appreciated.
Oct 4 '08 #7

P: n/a
SM
On Oct 3, 5:01*pm, Ivn Snchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
Michael Fesser wrote:
Only if the internal array pointer points to the first element. You
shouldn't rely on that and explicitly reset the array. If you want a
one-liner, write your own little helper function.

A helper function for just two sentences?

reset($array); echo key($array);

There you have, a one-liner. Maybe you want to use amore esoteric form,
depending on how you would need to catch errors:

reset($array) && echo key($array);

echo reset($array) ? key($array) : null;

Cheers,
--
----------------------------------
Ivn Snchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-3 generating this signature.
Uptime: 22:58:40 up 43 days, 10:54, *4 users, *load average: 1.14, 0.87,
0.62
Just for curiosity Ivan:
if this is a sample of my array:

....
$docs = array(
0=array(
y5Ub55UT=>'doc1',
cI8roXDw=>'doc2'
),
5=array(
HTMFQvUY=>'doc45',
x84vtG2e=>'doc28946'
)
);
....

and i want to access the 1st key of array 5

do i do it like this: reset($docs); echo key($docs[5]);
or
like this: reset($docs[5]); echo key($docs[5]);

is there a diference? I've tried both ways and it seems to work but
maybe one is better for some reason....
Thanks
Marco
Oct 4 '08 #8

P: n/a
..oO(SM)
>Just for curiosity Ivan:
if this is a sample of my array:

...
$docs = array(
0=array(
y5Ub55UT=>'doc1',
cI8roXDw=>'doc2'
),
5=array(
HTMFQvUY=>'doc45',
x84vtG2e=>'doc28946'
)
);
...

and i want to access the 1st key of array 5

do i do it like this: reset($docs); echo key($docs[5]);
or
like this: reset($docs[5]); echo key($docs[5]);
The second one.
>is there a diference? I've tried both ways and it seems to work but
maybe one is better for some reason....
Think logically about which array you're working with. You have a nested
array structure and want to work with one of the children, so it doesn't
make much sense to reset the parent array.

As an example take your array and use your both ways to print the first
key of array 5:

reset($docs); var_dump(key($docs[5]));
reset($docs[5]); var_dump(key($docs[5]));

Most likely you'll get the same result. Now let's say there's some work
going on on that array:

while ($data = each($docs[5])) {
//... do something
}

Nothing special. But now try to print the keys again:

reset($docs); var_dump(key($docs[5]));
reset($docs[5]); var_dump(key($docs[5]));

Micha
Oct 4 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.