kirgy
kirgy

Reputation: 1607

Getting PHP $_POST values by index?

I'm curious, more than anything - is it possible to get a PHP value posted? i.e. $_POST['foo'] via some kind of indexing? i.e. $_POST[0]?

($_POST[0] does not work by the way)

Upvotes: 1

Views: 10591

Answers (3)

raina77ow
raina77ow

Reputation: 106453

No, it's not possible: you cannot fetch values from an associative array by numeric indexes (because, as clearly noted in the doc, PHP does not distinguish between indexed and associative arrays).

That's why some functions (PDOStatement::fetch and its siblings, for example) that return arrays take an additional param to control the 'type' of indexes in the array returned: numeric (FETCH_NUM), string (FETCH_ASSOC) or both (FETCH_BOTH, the default value). )

The closest you might get with reindexing:

$myPost = array_values($_POST);

Upvotes: 6

davepmiller
davepmiller

Reputation: 2708

You can take data stored in $_POST variables and store them into indexed elements. But they are not stored as an indexed element initially.

Upvotes: 0

Landon
Landon

Reputation: 4108

not that i'm aware of, check out a print_r($_POST) to see all goodies that you can access. You could iterate over the values with:

foreach($_POST as $key=>$value){
    echo $key.' '.$value."\n";
}

You could throw in an $i++ if you wanted to keep track of a count ....

Upvotes: 5

Related Questions