php 从 CodeIgniter 中的 post 数组获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3112690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:43:04  来源:igfitidea点击:

Getting data from post array in CodeIgniter

phpcodeigniter

提问by Rocket Hazmat

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

好的,所以我有一个表单向我发送 POST 数组中的数组。我正在尝试像这样阅读它:

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

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

这不起作用。PHP 说“意外的 '['”。为什么这不起作用?

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.

我解决了我的问题,我只是好奇为什么第一种方法不起作用。

采纳答案by Daniel Egeberg

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:

PHP 不支持从函数调用中取消引用数组。它是在 PHP 的 SVN 主干版本中实现的,因此它很可能会进入 PHP 的未来版本。现在你将不得不求助于你现在正在做的事情。对于枚举数组,您还可以使用list

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

See: http://php.net/list

见:http: //php.net/list

回答by Sarfraz

Syntax like this:

像这样的语法:

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

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

PHP 不支持。你应该做你正在做的事情:

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

回答by Gerardo Jaramillo

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

另一种方法是使用foreach如下方式遍历数组:

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

回答by Ijas Ameenudeen

In addition to Daniel Egeberg's answer :

除了Daniel Egeberg的回答:

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

请注意,list()仅适用于数值数组。如果您/任何人想读取关联数组,例如,

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

use extract()function on above array as,

extract()在上面的数组上使用函数,

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.

注意:在上面的函数中,第一个参数是 post 数组,第二个参数是防止变量冲突,第三个参数是前缀。

For more on extract(), refer this.

有关更多信息extract(),请参阅

Hope this helps :)

希望这可以帮助 :)

回答by Raghu Acharya

I would always do like this..

我会一直这样做..

for($i=0; $i<count($this->input->post("days")); $i++)
{
  $day[$i] = $this->input->post("days[".$i."]");
}

This would be helpful if you need to interact with the db by checking each values passed by your view as an array. Otherwise I prefer foreach loop.

如果您需要通过检查视图作为数组传递的每个值来与数据库交互,这将很有帮助。否则我更喜欢 foreach 循环。

Cheers..

干杯..