2012-07-24 76 views
0

如何從下面的對象中獲取數組中的Cxyabc,Cxy123和Cxy234?SimpleXMLElement數組檢索問題

$xml_element = simplexml_load_string($xml,null, LIBXML_NOCDATA); 
$childId = $xml_element->Parent->ChildID; 

print_r(childId); 

SimpleXMLElement Object (
    [@attributes] => Array (
     [entity] => result 
     [order-value] => 1 
    ) 
    [0] => Cxyabc 
    [1] => Cxy123 
    [2] => Cxy234 
) 

感謝您的回答,我試過下面的一個,工作正常。字符串轉換是必要的。

$test = array(); 
foreach($childId as $value){ 
    $strValue = (string)$value; 
array_push($test,$strValue); 
} 
+1

你能發佈完整的代碼?然而'SimpleXMLElement'實現'Traversable',所以你可以使用'foreach($ instance-> children()as $ node){...}'來迭代內部集合實例。 – 2012-07-24 22:08:06

+1

@Praveen:這是一個SimpleXML對象,它很特別。 – 2012-07-24 22:28:06

+0

簡單但特別:) – 2012-07-24 22:34:48

回答

3

嘗試:

$cxyabc = $obj->{0}; 
$cxy123 = $obj->{1}; 

{ }的使用是必要的,因爲對象的屬性不能以數字開頭這樣$obj->0無效。

你會使用數組表示法訪問屬性:

$entity = $obj['entity'];