为什么不将这些值作为字符串添加到我的数组中?

| 对于这里的问题,我实际上想知道为什么我没有使用以下代码将字符串添加到数组中。 我从外部来源获得一些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中?     
已邀请:
  为什么$ i [\'src \']不被视为字符串? 因为它不是一个-它是一个SimpleXMLElement对象,如果在字符串上下文中使用,它将被强制转换为字符串,但其本质上仍然是一个SimpleXMLElement。 要使其成为真实的字符串,请强制将其强制转换为:
array_push($sources, (string) $i[\'src\']);  
    
因为
SimpleXMLElement::xpath()
(引用):   返回ѭ9的数组   对象 而不是字符串数组。 因此,
$images
数组中的项目是ѭ9and个对象,而不是字符串-这就是为什么想要字符串时必须将它们转换为字符串的原因。     

要回复问题请先登录注册