Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting all the keys for a map such as $_POST

Tags:

php

map

For example, say I post some data to a php file, but I don't know what the names of those values are. Where I would normally perform $_POST["username"] or something similar. How would I go about getting a list of all the key/value pairs within $_POST

like image 556
Tom Busby Avatar asked Jul 03 '11 23:07

Tom Busby


2 Answers

array_keys($_POST) will give you the array keys.

You can also do this to get values with key names:

foreach ($_POST as $key => $value) 
{
    //do stuff; 
}

However!!! Why wouldn't you know what keys are in the post? You don't want hackers putting random stuff into a post, sending it to you, and processing away. There is nothing preventing them from putting in 1000s of entries.

like image 57
evan Avatar answered Nov 16 '22 02:11

evan


Use array_keys to obtain all keys in $_POST super global array:

array_keys($_POST)

Simple example:

foreach (array_keys($_POST) as $key)
{
    print $_POST[$key];
}
like image 40
Grzegorz Szpetkowski Avatar answered Nov 16 '22 00:11

Grzegorz Szpetkowski