返回数组出现递归?
----------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解析是正确的啊,要不然也不会解出数组了。但是哪出问题了呢?
[解决办法]
$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-8if (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);