我需要幫助,我希望你能幫助我;) 我鼓勵一些大師尋求和平與知識嘿嘿。 好的讓我來解釋一下,我有一個XML,它有一個區段,其中的URL如下所示分段。我可以使用SimpleXML和XPath成功解析它。我的XPath查詢返回給我一個包含結果的數組,然後我可以回顯它們。沒有問題! =)解析XML並將分割的URL與SimpleXML和XPath連接起來
但是我想進一步嘗試減少代碼行並改進我的代碼。先來看看我的代碼,你可以複製/粘貼它,並給它一個嘗試:
<?php
$xml_g='
<files>
<file>
<filename>itzafile</filename>
<ext>.tar.gz</ext>
<url protocol="http://">itzanexample.net/folder/subfolder/</url>
</file>
<file>
<filename>itzavideo</filename>
<ext>.mp4</ext>
<url protocol="ftp://">itzanotherurl.com/videos/</url>
</file>
</files>
';
function URLparts($xml) {
$xmlData= simplexml_load_string("$xml");
$protocol = $xmlData->xpath('//file/url/@protocol');
$url = $xmlData->xpath('//file/url');
$filename = $xmlData->xpath('//file/filename');
$ext = $xmlData->xpath('//file/ext');
echo 'The following it\'s echoed calling 4 different arrays:'."\n\t".$protocol[0], $url[0], $filename[0], $ext[0]."\n\t".$protocol[1], $url[1], $filename[1], $ext[1]."\n\n"; //prints the entire url! Right!
//These variables are arrays, so theorically it must be possible to create an array of arrays here:
$completeURL = array($protocol,$url,$filename,$ext);
//I've also tried this but it's just the same problem:
//$completeURL = array(array($protocol),array($url),array($filename),array($ext));
echo 'The following should be echoed with a two-dimensional array, but something fails:'."\n\t".$completeURL[0][0][0][0]."\n\n"; //Just prints "http://" WRONG! u.u
/*
* $completeURL[1][0][0][0] prints the domain+subfolder
* $completeURL[0][1][0][0] prints ftp://
* $completeURL[0][0][1][0] prints nothing...
*/
}
URLparts($xml_g);
?>
正如你所看到的,我想避免加入網址爲:回聲$協議[0],$網址[0],$ filename [0],$ ext [0],我想用更少的變量來完成它,比如completeURL [0] [0] [0] [0](第一個文件節點及其所有相應的部分) ,completeURL [1] [1] [1] [1](第二個文件節點與所有url部分)等...
但顯然我做錯了什麼... 有人可以告訴我哪裏的錯誤??應有關多維數組我試圖創造...
很明顯,'echo'輸出結果是評估XPath表達式的節點集中每個節點的字符串值。知道執行'|'時,結果始終以文檔順序*顯示,這一點很重要。所以,'|'的參數順序並不重要。 – 2012-03-06 20:43:33
AH所以這就是原因! 「文件順序」謝謝你已經幫了我很多=) – Metafaniel 2012-03-06 22:21:45
Metafaniel:我很高興我一直有幫助。如果是這樣,你可以考慮更類似於感恩的表達(+1或接受)。 :) – 2012-03-06 23:11:06