views:

42

answers:

3

Ok, so I have a form that is sending me arrays in the POST array. I am trying to read it like so:

$day = $this->input->post("days")[0];

This does not work. PHP says "unexpected '['". Why does this not work?

I fixed it by doing it this way:

$days = $this->input->post("days");
$day = $days[0];

I fixed my problem, I'm just curious as to why the 1st way didn't work.

+2  A: 

Array derefencing from function calls isn't supported by PHP. It's implemented in the SVN trunk version of PHP, so it will likely make it into future versions of PHP. For now you'll have to resort to what you're doing now. For enumerated arrays you can also use list:

list($day) = $this->input->post("days");

See: http://php.net/list

Daniel Egeberg
Thanks. I'll stick with the way I'm doin' it now.
Rocket
+1  A: 

Syntax like this:

$day = $this->input->post("days")[0];

isn't supported in PHP. You should be doing what you are doing:

$days = $this->input->post("days");
$day = $days[0];
Sarfraz
A: 

another aproach could be to iterate trough te array witha foreach.

$foreach($this->input->post("days") as $day){
    echo $day;
}
Gerardo Jaramillo