For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > August 2005 > How to access second element of the array $_REQUEST?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to access second element of the array $_REQUEST?

2005-08-06, 8:59 am

Hello,
How to access second element of $_REQUEST?

I tried echo $_REQUEST[2] and got nothing, but when i check

p[rint_r($_REQUEST) then I see the array has surely more than 3 elements.

Regards,

Talthen


Janwillem Borleffs

2005-08-06, 8:59 am

talthen.z-serwera.o2@nospam.pl wrote:
> Hello,
> How to access second element of $_REQUEST?
>
> I tried echo $_REQUEST[2] and got nothing, but when i check
>
> p[rint_r($_REQUEST) then I see the array has surely more than 3
> elements.


$_REQUEST is not an indexed array, but associative. This means that values
can only be retrieved by keys.

Example:

The page is requested with a parameter foo with the value bar, as in:
page.php?foo=bar

If you want to print 'bar', you should do it like this:

print $_REQUEST['foo'];

But, you can get the values of the an associative array as an indexed array
through the array_values function:

$values = array_values($_REQUEST);

Afterwhich you can retrieve the values by their index:

print $value[1]; // 1 is the second index as 0 is always the first index

Please have a close look at the online manual starting at
http://www.php.net/manual/en/index.php to prevent asking basic questions
like this in the future.


JW



2005-08-06, 8:59 am

"Janwillem Borleffs" <jw@jwscripts.com>
> $_REQUEST is not an indexed array, but associative. This means that values
> can only be retrieved by keys.


Well... I thought it is both associative and indexed (like the $array
=mysql_fetch_rows which can be "BOTH").

> $values = array_values($_REQUEST);

Ok, thx.

Regards,
Talthen


Andy Hassall

2005-08-06, 8:59 am

On Sat, 6 Aug 2005 12:58:25 +0200, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:

>$_REQUEST is not an indexed array, but associative. This means that values
>can only be retrieved by keys.


Although to be picky, PHP associative arrays do preserve key ordering
(e.g.compare with Perl hashes, which don't), so there is a "value associated
with the second key", at least.

<?php
$a = array(
'a' => 'z',
'b' => 'y',
'c' => 'x'
);

$k = array_keys($a);
print $a[$k[1]];
?>

It seems possibly a bit dodgy to apply in relation to $_REQUEST since that can
be populated in various ways, not of which have ordering guarantees that I'm
sure enough about.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Andy Hassall

2005-08-06, 8:59 am

On Sat, 6 Aug 2005 12:58:25 +0200, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:

>$values = array_values($_REQUEST);


[ Just in case my previous reply that I've just sent a cancel message for gets
through - ignore it, I obviously only read half the post :p ]

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com