【问题标题】:split a array element by comma delimited string into an array? [closed]通过逗号分隔的字符串将数组元素拆分为数组? [关闭]
【发布时间】:2018-01-08 09:47:03
【问题描述】:
$data[10] = "12.21661,13.00128";
$loc = explode(",",$data[10]);
$lat = $loc[0];
$long = $loc[1]; **// Undefined offset:1**

当我运行上面的代码时,我在 $long = $loc[1];(Undefined offset:1) 上遇到错误。 我该如何解决这个问题??

【问题讨论】:

  • 代码工作正常,试试print_r($loc);告诉我们输出是什么。
  • 你能把所有的代码贴出来吗,因为我怀疑这只是一小部分,我猜这个处理是在循环中发生的
  • 数组 ([0] => 12.21661 [1] => 13.00128)
  • @JanardhanRam 请分享完整代码,偏移量 0 和 1 均已定义。所以这意味着问题不在您与我们共享的代码中。
  • 我找到了解决方案。我的代码工作正常。

标签: php arrays regex


【解决方案1】:

字符串中的数据为 2 位数字和 5 位小数。如果数字有固定限制,那么 PHP 中有函数:str_split()

这里有文档,看一个例子,就清楚多了。 它会像

一样以精确的长度分割

检查示例:Example #1 Example uses of str_split() in documentation http://php.net/manual/en/function.str-split.php

有explode()函数

http://php.net/manual/en/function.explode.php

我猜这正是你想要的。

看看,例子很简单。

回复任何查询评论。

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多