为什么不将这些值作为字符串添加到我的数组中?
|
对于这里的问题,我实际上想知道为什么我没有使用以下代码将字符串添加到数组中。
我从外部来源获得一些HTML:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = @simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath(\'//img\');
$sources = array();
这是图像数组:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[alt] => techcrunch logo
[src] => http://s2.wp.com/wp-content/themes/vip/tctechcrunch/images/logos_small/techcrunch2.png?m=1265111136g
)
)
...
)
然后,使用以下命令将源添加到数组中:
foreach ($images as $i) {
array_push($sources, $i[\'src\']);
}
但是当我打印结果时:
echo \"<pre>\";
print_r($sources);
die();
我得到这个:
Array
(
[0] => SimpleXMLElement Object
(
[0] => http://www.domain.com/someimages.jpg
)
...
)
为什么不将ѭ5视为字符串?原始的[src]元素不是在我在其中打印$ images的字符串的地方注明的吗?
换句话说,$ images [0]是SimpleXMLElement,我理解这一点。但是当我将其引用为“ 5”时,为什么那个THAT对象的\'src \'属性不是作为字符串而是出现在$ sources中?
没有找到相关结果
已邀请:
2 个回复
了驳
际恃啸称桅
(引用): 返回ѭ9的数组 对象 而不是字符串数组。 因此,
数组中的项目是ѭ9and个对象,而不是字符串-这就是为什么想要字符串时必须将它们转换为字符串的原因。