我们先来一段前奏: ?php $arr = Array(banana, apple, bear); echo $arr[0].br /; echo $arr[3]; ? 从上面可以看出, $arr [3]其实是不存在的,所以运行后出现了一条警告: Notice: Undefined offset: 3 in D:\wamp\apache\htdocs\test…… Undefined offse
我们先来一段前奏:
<?php $arr = Array("banana", "apple", "bear"); echo $arr[0]."<br />"; echo $arr[3]; ?>从上面可以看出,$arr[3]其实是不存在的,所以运行后出现了一条警告:
Notice: Undefined offset: 3 in D:\wamp\apache\htdocs\test……
Undefined offset,说明$arr[3],不存在,其超过了你所设置的数组长度。
再来看一段代码:
<?php header("Content-Type:text/html;charset=utf-8"); $sayings = "爱因斯坦||美国||探索真理比占有真理更为可贵。<|>". "亚里士多德||希腊||没有一个人能全面把握真理。". "达·芬奇||意大利||运动是一切生命的源泉。"; $data = explode("", $sayings); //explode返回一个数组 foreach($data as $line) { list($author, $nationality, $content) = explode("||", $line); echo $nationality."的 ".$author." 说 ".$content."<br>"; } ?>执行完后输出:
美国的 爱因斯坦 说 探索真理比占有真理更为可贵。
希腊的 亚里士多德 说 没有一个人能全面把握真理。
意大利的 达·芬奇 说 运动是一切生命的源泉。
Notice: Undefined offset: 2 in D:\wamp\apache\htdocs\test\explode.php on
line 29
Notice: Undefined offset: 1 in D:\wamp\apache\htdocs\test\explode.php on
line 29
的 说
这是为什么呢?我找了很久,终于发现错误出现在$data = explode("", $sayings);
就是这段代码,还是数组的问题。explode()返回一个函数:
array explode ( string $delimiter
, string $string
[, int $limit
]
)
让我们打印出$data:
Array
(
[0] => 爱因斯坦||美国||探索真理比占有真理更为可贵。
[1] => 亚里士多德||希腊||没有一个人能全面把握真理。
[2] => 达·芬奇||意大利||运动是一切生命的源泉。
[3] =>
)
发现没有,$data[3]为空值,这为下面使用list()函数,然后list()函数在解析$data[3]时就出现offset了。
offset就是你所引用的那个数组值实际不存在,所以出现Undefined offset
就是$sayings 字符串的内容后面多了一个"",导致在用explode()函数时出现了$data[3]这个空值。所以在使用explode()函数时注意了。
解决办法就是改一段代码:$data = explode("", $sayings);
$data = explode("", $sayings, 3);
或在这段代码下面加上 unset($data[count($data)-1]); 就是得把那个空数组值变量给去掉。具体的,读者多多体会吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
