2013-10-04 86 views
0

這個新手並不能找出如何引用元素。用PHP尋址SimpleXML元素

我正在使用PHP,並希望能夠將First_Name中的值放入HTML表格中的單元格。

感謝您的任何幫助。

SNIPPIT輸出的:

SimpleXMLElement Object 
(
[@attributes] => Array 
    (
     [organization_KEY] => 5728 
    ) 

[supporter-supporter_KEY-supporter_groups] => SimpleXMLElement Object 
    (
     [item] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [supporter_KEY] => 6147839 
         [organization_KEY] => 12345 
         [chapter_KEY] => SimpleXMLElement Object 
          (
          ) 

         [Last_Modified] => Thu Jul 09 2009 15:11:39 GMT-0400 (EDT) 
         [Date_Created] => Thu Jul 09 2009 14:44:22 GMT-0400 (EDT) 
         [Title] => Mr. 
         [First_Name] => Michael 
         [MI] => SimpleXMLElement Object 
          (

---------------------------------------------------- 

php ---------------------------------------------------- 
curl_setopt($ch, CURLOPT_URL, "$url/getLeftJoin.sjs"); 
$fields = array('object' => 'supporter(supporter_KEY)supporter_groups',  'condition'=>'supporter_KEY=123456'); 
echo (http_build_query($fields)); 
//return the transfer as a string 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); 
// $output contains the output string 
$output = curl_exec($ch); 
//Populate results from salsa into SimpleXML object 
// See http://us3.php.net/manual/en/book.simplexml.php for more information 
// on SimpleXML objects in PHP 
$data = simplexml_load_string($output); 
**echo ("data ". $data->supporter->First_Name);** 

回答

0
項目

是一個數組,所以可以在陣列中尋址第一元件:

$ DATA-> supporter->項[0] - >將First_Name

http://www.php.net/manual/en/simplexml.examples-basic.php

嘗試:

echo $ data - > {'supporter-supporter_KEY-supporter_groups'} - > item - > [0] - > First_Name;

大括號阻止對用作減法的連字符的任何解釋。

+0

稍微調整一下.. echo $ data - > {'supporter-supporter_KEY-supporter_groups'} - > item-> First_Name – user2800165

+0

是的好點! – webbo