将 php 代码转换为数组
我使用 codeigniter,我将两个输入作为数组发送到以下 php 代码,但是以下 php 代码没有将其作为数组获取,如何更改 php 代码以获取值数组?
<input type="text" name="date[]">
<input type="text" name="date[]">
$date = $this -> input -> post('date');
$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);
list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97
if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
return '0';
}
使用上面的代码我有这个错误:
遇到 PHP 错误
严重性:通知
消息:数组到字符串的转换
文件名:admin/model.php
行号:97
-
遇到 PHP 错误
严重性:通知
消息:未定义的偏移量:2
文件名:admin/model.php
行号:97
-
遇到 PHP 错误
严重性:通知
消息:未定义的偏移量:1
文件名:admin/model.php
行号:97
I use codeigniter, i send tow input as array to following php code, but following php code don't get it as array, how change php code for get value array?
<input type="text" name="date[]">
<input type="text" name="date[]">
$date = $this -> input -> post('date');
$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);
list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97
if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
return '0';
}
with above code i have this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: admin/model.php
Line Number: 97
-
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: admin/model.php
Line Number: 97
-
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: admin/model.php
Line Number: 97
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要阅读并理解错误消息:
这意味着您正在将数组转换为字符串。这通常会产生以下字符串:
"Array"
:在
"Array"
字符串上使用explode
返回以下内容:所以一个数组索引
0
上包含一个包含"Array"
的字符串。所以:结果:
显然这不是你想要的。我不知道你想要什么,也许是这个?:
但也许不是,因为你写道你有两个日期字段,而不是三个。另请参阅Marc B 的回答。
You need to read and understand the error messages:
Meaning you're converting an array to a string. That normally results in the following string :
"Array"
:Using that
explode
on a the"Array"
string returns the following:So an array with one string containing
"Array"
on index0
. So:Results in:
Obviously this is not what you wanted. I don't know what you wanted, maybe this?:
But maybe not, because you wrote you have two date fields, not three. See as well Marc B's answer.
在该行运行时,$date 是一个数组,而不是字符串。你不能爆炸一个数组。如果我正确理解您的代码,您可能会想要执行以下操作:
并分别处理从表单提交的每个“日期”。
At the point this line runs, $date is an array, not a string. You can't explode an array. If I'm understanding your code correctly, you'd probably want to do:
and process each of the "dates" submitted from the form separately.
据我所知, $date 已经是一个数组。
因此,您需要 3 个输入字段。
那么 $date 应该已经是一个数组了。检查
var_dump($this->input->post('date'));
From what I see, $date is already an array.
so, you need 3 input fields.
then $date should be an array already. check
var_dump($this->input->post('date'));