Take the 2-minute tour ×

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.

share|improve this question
Here's some SO discussion on the topic: stackoverflow.com/questions/742764/… –  Dan U. Jun 24 '10 at 18:13

4 Answers 4

up vote 6 down vote accepted

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

share|improve this answer
Thanks. I'll stick with the way I'm doin' it now. –  Rocket Hazmat Jun 24 '10 at 18:16
In addition to this answer, Please check this stackoverflow.com/a/22652521/567854 also in case the array is an associative array :) –  IJas Apr 4 at 3:50

Syntax like this:


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

$day = $days[0];
share|improve this answer

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

$foreach($this->input->post("days") as $day){
    echo $day;
share|improve this answer

In addition to Daniel Egeberg's answer :

Please note that list() only works with numerical arrays. If you/anyone want to read an associative array like,

$_POST['date'] = array
                    'day'   => 12
                    'month' => 7
                    'year'  => 1986

use extract() function on above array as,

extract($this->input->post("date"), EXTR_PREFIX_ALL, "date");

Now the following variables will be available to use as,

$date_day = 19, $date_month = 7 and $date_year = 1986

NOTE: in the above function, first argument is the post array, second one is to protect from variable collisions and the third is the prefix.

For more on extract(), refer this.

Hope this helps :)

share|improve this answer

Your Answer


