返回数组出现递归?

----------PHP----------
$mail_list = parse_xml_config($inx, "mailindex");
$receives = $mail_list['datas']['data'];
$sumCount = count($receives); //当前邮箱中的邮件总数
echo "count-->>$sumCount<br/>";
print_r($receives);
---------XML-----------
<?xml version="1.0" encoding="utf-8"?>
<mailindex>
<datas>
<data>
<Return-Path><anonymous@214.netchina.com.cn></Return-Path>
<Delivered-To>yushuai.niu@214.netchina.com.cn</Delivered-To>
<Received>(ncmail 843 invoked by uid 401); 027 Feb 2012 22:43:50 -0000</Received>
<Date>027 Feb 2012 22:43:50 -0000</Date>
<Message-ID><20120227224350.842.ncmail@214.netchina.com.cn></Message-ID>
<To>yushuai.niu@214.netchina.com.cn</To>
<Subject>hhh</Subject>
<X-PHP-Originating-Script>0:send_mode_class.php</X-PHP-Originating-Script>
<From>yushuai.niu@214.netchina.com.cn</From>
<X-Level>秘密</X-Level>
<X-Encrypt>----====_1330382630_====----</X-Encrypt>
<MIME-version>1.0</MIME-version>
<Content-type>multipart/mixed;boundary="--==================_117335625==_"</Content-type>
<Time>2012-02-27 22:43:50</Time>
<Size>24.16 KB</Size>
<Attachs>
<attach>
<id>2</id>
<name>netchina_1 ReleasedBy_sean(Data_2012-2-21).doc</name>
<level>秘密</level>
</attach>
</Attachs>
<bodyId>2</bodyId>
</data>
<data>
<Return-Path><anonymous@214.netchina.com.cn></Return-Path>
<Delivered-To>yushuai.niu@214.netchina.com.cn</Delivered-To>
<Received>(ncmail 23267 invoked by uid 401); 027 Feb 2012 20:21:57 -0000</Received>
<Date>027 Feb 2012 20:21:57 -0000</Date>
<Message-ID><20120227202157.23265.ncmail@214.netchina.com.cn></Message-ID>
<To>yushuai.niu@214.netchina.com.cn</To>
<Subject>w-inx-2</Subject>
<X-PHP-Originating-Script>0:send_mode_class.php</X-PHP-Originating-Script>
<From>yushuai.niu@214.netchina.com.cn</From>
<X-Level>绝密</X-Level>
<X-Encrypt>----====_1330374117_====----</X-Encrypt>
<MIME-version>1.0</MIME-version>
<Content-type>multipart/mixed;boundary="--==================_783205184==_"</Content-type>
<Time>2012-02-27 20:21:57</Time>
<Size>42.06 KB</Size>
<Attachs>
<attach>
<id>2</id>
<name>2012_2_1_15_15.doc</name>
<level>绝密</level>
</attach>
<attach>
<id>3</id>
<name>netchina.doc</name>
<level>绝密</level>
</attach>
</Attachs>
<bodyId>3</bodyId>
</data>
</datas>
</mailindex>
--------------打印输出----------
count-->>1
Array ( [0] => Array ( [return-path] => anonymous@214.netchina.com.cn [delivered-to] => yushuai.niu@214.netchina.com.cn [received] => (ncmail 843 invoked by uid 401); 027 Feb 2012 22:43:50 -0000 [date] => 027 Feb 2012 22:43:50 -0000 [message-id] => 20120227224350.842.ncmail@214.netchina.com.cn [to] => yushuai.niu@214.netchina.com.cn [subject] => hhh [x-php-originating-script] => 0:send_mode_class.php [from] => yushuai.niu@214.netchina.com.cn [x-level] => 秘密 [x-encrypt] => ----====_1330382630_====---- [mime-version] => 1.0 [content-type] => multipart/mixed;boundary=--==================_117335625==_ [time] => 2012-02-27 22:43:50 [size] => 24.16 KB [attachs] => Array ( [attach] => Array ( [0] => Array ( [id] => 2 [name] => netchina_1 ReleasedBy_sean(Data_2012-2-21).doc [level] => 秘密 ) ) ) [bodyid] => 2 ) )
count-->>2
Array ( [0] => Array ( [return-path] => anonymous@214.netchina.com.cn [delivered-to] => yushuai.niu@214.netchina.com.cn [received] => (ncmail 843 invoked by uid 401); 027 Feb 2012 22:43:50 -0000 [date] => 027 Feb 2012 22:43:50 -0000 [message-id] => 20120227224350.842.ncmail@214.netchina.com.cn [to] => yushuai.niu@214.netchina.com.cn [subject] => hhh [x-php-originating-script] => 0:send_mode_class.php [from] => yushuai.niu@214.netchina.com.cn [x-level] => 秘密 [x-encrypt] => ----====_1330382630_====---- [mime-version] => 1.0 [content-type] => multipart/mixed;boundary=--==================_117335625==_ [time] => 2012-02-27 22:43:50 [size] => 24.16 KB [attachs] => Array ( [attach] => Array ( [0] => Array ( [id] => 2 [name] => netchina_1 ReleasedBy_sean(Data_2012-2-21).doc [level] => 秘密 ) ) ) [bodyid] => 2 ) [1] => Array ( [return-path] => anonymous@214.netchina.com.cn [delivered-to] => yushuai.niu@214.netchina.com.cn [received] => (ncmail 23267 invoked by uid 401); 027 Feb 2012 20:21:57 -0000 [date] => 027 Feb 2012 20:21:57 -0000 [message-id] => 20120227202157.23265.ncmail@214.netchina.com.cn [to] => yushuai.niu@214.netchina.com.cn [subject] => w-inx-2 [x-php-originating-script] => 0:send_mode_class.php [from] => yushuai.niu@214.netchina.com.cn [x-level] => 绝密 [x-encrypt] => ----====_1330374117_====---- [mime-version] => 1.0 [content-type] => multipart/mixed;boundary=--==================_783205184==_ [time] => 2012-02-27 20:21:57 [size] => 42.06 KB [attachs] => Array ( [attach] => Array ( [0] => Array ( [id] => 2 [name] => 2012_2_1_15_15.doc [level] => 绝密 ) [1] => Array ( [id] => 3 [name] => netchina.doc [level] => 绝密 ) ) ) [bodyid] => 3 ) )
-------------xml解析--------------
function parse_xml_config($cffile, $rootobj) {
global $depth, $curpath, $config, $havedata, $listtags;

$config = array();
$curpath = array();
$depth = 0;
$havedata = 0;
$encoding = "UTF-8"; // PHP default

// Create XML parser and initialize handler.
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "cdata");

// Read configuration file.
$data = file_get_contents($cffile);
if (FALSE === $data)
die("Error: 不能打开 XML 文件\n");

// Detect and set output character encoding. Default output encoding is UTF-8
if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $data, $m)) {
$encoding = strtoupper($m[1]);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $encoding);
}

// Parse configuration.
if (!xml_parse($xml_parser, $data)) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

if (!$config[$rootobj]) {
die("XML error: $rootobj 根不存在!\n");
}

// Remember encoding
$config[$rootobj]['encoding'] = $encoding;

return $config[$rootobj];
}

-------------
为什么返回的数组$mail_list会是递归出现的?xml解析是正确的啊,要不然也不会解出数组了。但是哪出问题了呢?
...全文
370 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

$listtags = explode(" ", "list data attach user");

function startElement($parser, $name, $attrs) {
global $depth, $curpath, $config, $havedata, $listtags;

array_push($curpath, strtolower($name));

$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}

/* is it an element that belongs to a list? */
if (in_array(strtolower($name), $listtags)) {
/* is there an array already? */
if (!is_array($ptr)) {
/* make an array */
$ptr = array();
}

array_push($curpath, count($ptr));

} else if (isset($ptr)) {
/* multiple entries not allowed for this element, bail out
* 不允许元素多次出境,否则溢出*/
//die(sprintf("XML error: %s at line %d cannot occur more than once \n",
die(sprintf("XML error: %s 在第 %d 行, 超过一次就不再出现\n",
$name,
xml_get_current_line_number($parser)));
}

$depth++;
$havedata = $depth;
}

function endElement($parser, $name) {
global $depth, $curpath, $config, $havedata, $listtags;

if ($havedata == $depth) {
$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}
$ptr = "";
}

array_pop($curpath);

if (in_array(strtolower($name), $listtags))
array_pop($curpath);

$depth--;
}

function cData($parser, $data) {
global $depth, $curpath, $config, $havedata;

$data = trim($data, "\t\n\r");

if ($data != "") {
$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}

if (is_string($ptr)) {
$ptr .= $data;
} else {
if (trim($data, " ") != "") {
$ptr = $data;
$havedata++;
}
}
}
}

续解……
xuzuning 2012-02-28
  • 打赏
  • 举报
回复
函数 startElement、endElement、cdata 未给出,不好说什么
wangliwei230 2012-02-28
  • 打赏
  • 举报
回复
鼓掌 受教了!!!!!!!!!
  • 打赏
  • 举报
回复
真的非常感谢您的帮助,我把parse_xml_config()放到循环里了,所以才一直出现递归数组。
不愧是PHP中的巨擘!
以后要多向您学习了!
xuzuning 2012-02-28
  • 打赏
  • 举报
回复
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<mailindex>
<datas>
<data>
<Return-Path><anonymous@214.netchina.com.cn></Return-Path>
<Delivered-To>yushuai.niu@214.netchina.com.cn</Delivered-To>
<Received>(ncmail 843 invoked by uid 401); 027 Feb 2012 22:43:50 -0000</Received>
<Date>027 Feb 2012 22:43:50 -0000</Date>
<Message-ID><20120227224350.842.ncmail@214.netchina.com.cn></Message-ID>
<To>yushuai.niu@214.netchina.com.cn</To>
<Subject>hhh</Subject>
<X-PHP-Originating-Script>0:send_mode_class.php</X-PHP-Originating-Script>
<From>yushuai.niu@214.netchina.com.cn</From>
<X-Level>秘密</X-Level>
<X-Encrypt>----====_1330382630_====----</X-Encrypt>
<MIME-version>1.0</MIME-version>
<Content-type>multipart/mixed;boundary="--==================_117335625==_"</Content-type>
<Time>2012-02-27 22:43:50</Time>
<Size>24.16 KB</Size>
<Attachs>
<attach>
<id>2</id>
<name>netchina_1 ReleasedBy_sean(Data_2012-2-21).doc</name>
<level>秘密</level>
</attach>
</Attachs>
<bodyId>2</bodyId>
</data>
<data>
<Return-Path><anonymous@214.netchina.com.cn></Return-Path>
<Delivered-To>yushuai.niu@214.netchina.com.cn</Delivered-To>
<Received>(ncmail 23267 invoked by uid 401); 027 Feb 2012 20:21:57 -0000</Received>
<Date>027 Feb 2012 20:21:57 -0000</Date>
<Message-ID><20120227202157.23265.ncmail@214.netchina.com.cn></Message-ID>
<To>yushuai.niu@214.netchina.com.cn</To>
<Subject>w-inx-2</Subject>
<X-PHP-Originating-Script>0:send_mode_class.php</X-PHP-Originating-Script>
<From>yushuai.niu@214.netchina.com.cn</From>
<X-Level>绝密</X-Level>
<X-Encrypt>----====_1330374117_====----</X-Encrypt>
<MIME-version>1.0</MIME-version>
<Content-type>multipart/mixed;boundary="--==================_783205184==_"</Content-type>
<Time>2012-02-27 20:21:57</Time>
<Size>42.06 KB</Size>
<Attachs>
<attach>
<id>2</id>
<name>2012_2_1_15_15.doc</name>
<level>绝密</level>
</attach>
<attach>
<id>3</id>
<name>netchina.doc</name>
<level>绝密</level>
</attach>
</Attachs>
<bodyId>3</bodyId>
</data>
</datas>
</mailindex>
XML;

function parse_xml_config($cffile, $rootobj) {
global $depth, $curpath, $config, $havedata, $listtags;

$config = array();
$curpath = array();
$depth = 0;
$havedata = 0;
$encoding = "UTF-8"; // PHP default

// Create XML parser and initialize handler.
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "cdata");

// Read configuration file.
//$data = file_get_contents($cffile);
$data = $cffile;
if (FALSE === $data)
die("Error: 不能打开 XML 文件\n");

// Detect and set output character encoding. Default output encoding is UTF-8
if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $data, $m)) {
$encoding = strtoupper($m[1]);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $encoding);
}

// Parse configuration.
if (!xml_parse($xml_parser, $data)) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

if (!$config[$rootobj]) {
die("XML error: $rootobj 根不存在!\n");
}

// Remember encoding
$config[$rootobj]['encoding'] = $encoding;

return $config[$rootobj];
}

$listtags = explode(" ", "list data attach user");

function startElement($parser, $name, $attrs) {
global $depth, $curpath, $config, $havedata, $listtags;

array_push($curpath, strtolower($name));

$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}

/* is it an element that belongs to a list? */
if (in_array(strtolower($name), $listtags)) {
/* is there an array already? */
if (!is_array($ptr)) {
/* make an array */
$ptr = array();
}

array_push($curpath, count($ptr));

} else if (isset($ptr)) {
/* multiple entries not allowed for this element, bail out
* 不允许元素多次出境,否则溢出*/
//die(sprintf("XML error: %s at line %d cannot occur more than once \n",
die(sprintf("XML error: %s 在第 %d 行, 超过一次就不再出现\n",
$name,
xml_get_current_line_number($parser)));
}

$depth++;
$havedata = $depth;
}

function endElement($parser, $name) {
global $depth, $curpath, $config, $havedata, $listtags;

if ($havedata == $depth) {
$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}
$ptr = "";
}

array_pop($curpath);

if (in_array(strtolower($name), $listtags))
array_pop($curpath);

$depth--;
}

function cData($parser, $data) {
global $depth, $curpath, $config, $havedata;

$data = trim($data, "\t\n\r");

if ($data != "") {
$ptr =& $config;
foreach ($curpath as $path) {
$ptr =& $ptr[$path];
}

if (is_string($ptr)) {
$ptr .= $data;
} else {
if (trim($data, " ") != "") {
$ptr = $data;
$havedata++;
}
}
}
}

$inx = iconv('gbk', 'utf-8', $xml);//由于我是在gbk环境中工作,所以要转一下码
$mail_list = parse_xml_config($inx, "mailindex");
$receives = $mail_list['datas']['data'];
$sumCount = count($receives); //当前邮箱中的邮件总数
echo "count-->>$sumCount<br/>";
print_r($receives);
  • 打赏
  • 举报
回复
真的吗?我的怎么会不一样呢?能把您的php调用那段代码贴来看看吗?我想比较一下哪错了,已经找了很久了,一直是出现数组递归……
xuzuning 2012-02-28
  • 打赏
  • 举报
回复
我用的就是你的代码和数据
除了不是从文件读出外,未做其他改动
  • 打赏
  • 举报
回复
咦?你这个数组怎么出来的?我要的就是这个数组啊,但是我写了一个print_r($mail_list)打出来的数组,却是两个啊。

Array
(
[0] => Array
(
[return-path] => <anonymous@214.netchina.com.cn>
[delivered-to] => yushuai.niu@214.netchina.com.cn……
)
)
Array
(
[0] => Array
(
[return-path] => <anonymous@214.netchina.com.cn>
[delivered-to] => yushuai.niu@214.netchina.com.cn……
)
[1] => Array
(
[return-path] => <anonymous@214.netchina.com.cn>
[delivered-to] => yushuai.niu@214.netchina.com.cn……
)
)

我那里写错了吗?
xuzuning 2012-02-28
  • 打赏
  • 举报
回复
你到底要的是什么
Array
(
[0] => Array
(
[return-path] => <anonymous@214.netchina.com.cn>
[delivered-to] => yushuai.niu@214.netchina.com.cn
[received] => (ncmail 843 invoked by uid 401); 027 Feb 2012 22:43:50 -0000
[date] => 027 Feb 2012 22:43:50 -0000
[message-id] => <20120227224350.842.ncmail@214.netchina.com.cn>
[to] => yushuai.niu@214.netchina.com.cn
[subject] => hhh
[x-php-originating-script] => 0:send_mode_class.php
[from] => yushuai.niu@214.netchina.com.cn
[x-level] => 秘密
[x-encrypt] => ----====_1330382630_====----
[mime-version] => 1.0
[content-type] => multipart/mixed;boundary="--==================_117335625==_"
[time] => 2012-02-27 22:43:50
[size] => 24.16 KB
[attachs] => Array
(
[attach] => Array
(
[0] => Array
(
[id] => 2
[name] => netchina_1 ReleasedBy_sean(Data_2012-2-21).doc
[level] => 秘密
)

)

)

[bodyid] => 2
)

[1] => Array
(
[return-path] => <anonymous@214.netchina.com.cn>
[delivered-to] => yushuai.niu@214.netchina.com.cn
[received] => (ncmail 23267 invoked by uid 401); 027 Feb 2012 20:21:57 -0000
[date] => 027 Feb 2012 20:21:57 -0000
[message-id] => <20120227202157.23265.ncmail@214.netchina.com.cn>
[to] => yushuai.niu@214.netchina.com.cn
[subject] => w-inx-2
[x-php-originating-script] => 0:send_mode_class.php
[from] => yushuai.niu@214.netchina.com.cn
[x-level] => 绝密
[x-encrypt] => ----====_1330374117_====----
[mime-version] => 1.0
[content-type] => multipart/mixed;boundary="--==================_783205184==_"
[time] => 2012-02-27 20:21:57
[size] => 42.06 KB
[attachs] => Array
(
[attach] => Array
(
[0] => Array
(
[id] => 2
[name] => 2012_2_1_15_15.doc
[level] => 绝密
)

[1] => Array
(
[id] => 3
[name] => netchina.doc
[level] => 绝密
)

)

)

[bodyid] => 3
)

)
  • 打赏
  • 举报
回复
我只想得到:count-->>2的那个数组,正常情况应该是array([0]=>1,[1]=>2,[2]=>3)
但是,我只print_r(),它返回的是
array([0]=>1)
array([0]=>1,[1]=>2)
array([0]=>1,[1]=>2,[2]=>3)
这是什么情况呢?第一次见,应该怎么取到最后那个数组呢?这样子描述不知清楚否?
xuzuning 2012-02-28
  • 打赏
  • 举报
回复
你得到的结果不是正确的吗?

你希望得到的是什么样的结果?
  • 打赏
  • 举报
回复
各位大侠!!!来看看那,分布式问题,问题是解决问题的方法,来看看啊。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧