将 php 代码转换为数组

发布于 2024-12-04 11:20:37 字数 1100 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-12-11 11:20:37

您需要阅读并理解错误消息:

Message: Array to string conversion (Line 97)

这意味着您正在将数组转换为字符串。这通常会产生以下字符串: "Array"

list($year, $month, $day) = explode('/', "Array", 3);//Line Number: 97

"Array" 字符串上使用 explode 返回以下内容:

array(1) {
  [0]=>
  string(5) "Array"
}

所以一个数组索引 0 上包含一个包含 "Array" 的字符串。所以:

list($year, $month, $day) = array('Array');

结果:

$year = "Array"; $month = NULL; $day = NULL;

显然这不是你想要的。我不知道你想要什么,也许是这个?:

list($year, $month, $day) = $date;

但也许不是,因为你写道你有两个日期字段,而不是三个。另请参阅Marc B 的回答

You need to read and understand the error messages:

Message: Array to string conversion (Line 97)

Meaning you're converting an array to a string. That normally results in the following string : "Array":

list($year, $month, $day) = explode('/', "Array", 3);//Line Number: 97

Using that explode on a the "Array" string returns the following:

array(1) {
  [0]=>
  string(5) "Array"
}

So an array with one string containing "Array" on index 0. So:

list($year, $month, $day) = array('Array');

Results in:

$year = "Array"; $month = NULL; $day = NULL;

Obviously this is not what you wanted. I don't know what you wanted, maybe this?:

list($year, $month, $day) = $date;

But maybe not, because you wrote you have two date fields, not three. See as well Marc B's answer.

傲世九天 2024-12-11 11:20:37
list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97

在该行运行时,$date 是一个数组,而不是字符串。你不能爆炸一个数组。如果我正确理解您的代码,您可能会想要执行以下操作:

foreach ($date as $d) {
    list($year, $month, $day) = explode('/', $d, 3);
}

并分别处理从表单提交的每个“日期”。

list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97

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:

foreach ($date as $d) {
    list($year, $month, $day) = explode('/', $d, 3);
}

and process each of the "dates" submitted from the form separately.

倾`听者〃 2024-12-11 11:20:37

据我所知, $date 已经是一个数组。

因此,您需要 3 个输入字段。

<input type="text" name="date[]">
<input type="text" name="date[]">
<input type="text" name="date[]">

那么 $date 应该已经是一个数组了。检查 var_dump($this->input->post('date'));

$date = $this -> input -> post('date');

$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);

$year = $date[0];
$month = $date[1];
$day = $date[2];

if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
  return '0';
}

From what I see, $date is already an array.

so, you need 3 input fields.

<input type="text" name="date[]">
<input type="text" name="date[]">
<input type="text" name="date[]">

then $date should be an array already. check var_dump($this->input->post('date'));

$date = $this -> input -> post('date');

$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);

$year = $date[0];
$month = $date[1];
$day = $date[2];

if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
  return '0';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文