我有一些代碼,從外部源拉HTML:爲什麼我在這裏獲取一個SimpleXMLElement對象數組?
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = @simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//img');
$sources = array();
然後,如果我添加的所有來源與此代碼:
foreach ($images as $i) {
array_push($sources, $i['src']);
}
echo "<pre>";
print_r($sources);
die();
我得到這樣的結果:
Array
(
[0] => SimpleXMLElement Object
(
[0] => /images/someimage.gif
)
[1] => SimpleXMLElement Object
(
[0] => /images/en/someother.jpg
)
....
)
但是,當我使用此代碼:
foreach ($images as $i) {
$sources[] = (string)$i['src'];
}
我得到這樣的結果(這是什麼的話):
Array
(
[0] => /images/someimage.gif
[1] => /images/en/someother.jpg
...
)
是什麼導致了這種差異? array_push()有什麼不同?
感謝,
編輯:雖然我知道答案匹配我問什麼(我已經頒發),我更想知道爲什麼無論是使用array_push或其他符號增加了SimpleXMLElement對象,而不是一個字符串,當兩個沒有鑄造。我知道當明確地轉換爲字符串時,我會得到一個字符串。看到這裏跟進的問題:Why aren't these values being added to my array as strings?
謝謝 - 我已經發布了一個跟進的問題,如果你會這麼好心。我更加期待找出什麼時候我不投,我沒有得到一個字符串添加。 – barfoon 2011-04-14 19:27:21