Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from post array in CodeIgniter

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.

like image 662
Rocket Hazmat Avatar asked Jun 24 '10 18:06

Rocket Hazmat


2 Answers

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];
like image 45
Sarfraz Avatar answered Sep 18 '22 04:09

Sarfraz


Another approach could be to iterate through the array by using foreach like so:

foreach($this->input->post("days") as $day){
    echo $day;
}
like image 196
Gerardo Jaramillo Avatar answered Sep 18 '22 04:09

Gerardo Jaramillo