2017-02-04 51 views
0
<function name="getMemberInfo"> 
    <component name="people"> 
     <property name="ppl_firstname"> 
      <![CDATA[ Jamie ]]> 
     </property> 
    </component> 
</function> 

我試圖檢索ppl_firstname。以上顯示的XML是已由API url解析的代碼的一部分。如何在PHP中使用數組獲取值元素[@attributes]

我用捲曲,以便獲得url內容

$url="" 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents 
$data = curl_exec($ch); // execute curl request 
curl_close($ch); 

然後我已經把數據在陣列

$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); 
$xmlJson = json_encode($xml); 
$xmlArr = json_decode($xmlJson, 1); // Returns associative array 

,其輸出是這樣的:

Array ([@attributes] => 
    Array ([name] => getMemberInfo) [component] => 
     Array ([0] => 
      Array ([@attributes] => 
       Array ([name] => people) [property] => 
        Array ([0] => Jamie [1]) 
       ) 
      ) 
     ) 

人告訴我如何找回名字。在此先感謝

+0

什麼名字? 'Jamie'? 'people'? 'memberInfo'? –

+0

謝謝@u_mulder我需要屬性值<![CDATA [Jamie]]> –

回答

0

你要做的是首先訪問孩子property然後你可以訪問這個孩子的屬性。

$xml_string = '<function name="getMemberInfo"> 
        <component name="people"> 
          <property name="ppl_firstname"> 
           <![CDATA[ Jamie ]]> 
          </property> 
        </component> 
       </function>'; 
$function = simplexml_load_string($xml_string); 
$component= $function->children(); 
//access your property children 
$property = $component->children(); 
$property_name = $property->attributes()->name; 
//check if it prints the right name 
echo "Property name: ".$property_name 
+0

謝謝你們完美的工作! –

+0

我很高興它做到了! @JamieYouell – kampangala

相關問題