1

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)

ata
  • 3,398
  • 5
  • 20
  • 31
kirgy
  • 1,567
  • 6
  • 23
  • 39
  • $POST's cannot be accessed as an index directly. But here is a cheap and simple solution: $myPost = array_values($_POST); echo $myPost[0]; This will echo the first value posted. – kirgy Oct 23 '12 at 22:50
  • 1
    I actually don't see the use for accessing $_POST values by numeric index - would anyone care to enlighten me on a case where it would be practical? – nyson Oct 23 '12 at 22:56
  • You usually do not want to care about the order of values, they highly depend on influencing factors like order of input elements in HTML - which might change because of layout changes. Do not depend on them, use the names - because you should know them, it is your form after all. – Sven Oct 23 '12 at 22:57
  • Sure; I've been considering creating an API for a piece of online software im developing. When posting to the API one function may require 2 values, another 5. Sending a post to the API which then posts values to a relevant function would be difficult unless we know the names of all the relevant post. In addition we then start setting a load of "null" in various function variables if we were to use field names of non meaningful values (postVar1, postVar2...). I figured I could extract a "function name" from post[0], then post the rest of that array to a relevant function via a "switch, case". – kirgy Oct 23 '12 at 23:04
  • ^ I think that made sense...I have trouble getting thoughts out of the head sometimes! @Sven, youre right. I wouldnt normally use it, especially not in a normal form. The way I intend to use it is via another webpage which posts to my api script. I would give it a format requirement, i.e. $function, $var1, $var2, $var3 ...etc. – kirgy Oct 23 '12 at 23:09
  • 1
    You could name several inputs `name="postVar[]"`, this will make you able to access the variables by `$_POST[postVar][x]`, where x is an integer and be able to iterate over them! – nyson Oct 24 '12 at 00:03

3 Answers3

6

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);
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Ah I see...I've never really looked at a POST as an array until just now...your solution provides a more than ample answer - thank you! – kirgy Oct 23 '12 at 22:47
5

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 ....

Landon
  • 4,088
  • 3
  • 28
  • 42
0

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

davepmiller
  • 2,620
  • 3
  • 33
  • 61