2010-06-18 3 views
4

J'exécuter un code comme ceci:Comment accéder aux attributs avec des tirets dans le nom?

$quote = simplexml_load_string($xml); 
$quote = $quote->Stock; 
echo 'Name: '; 
echo $quote->Name; 
echo '<br>'; 
echo 'Sybmol: '; 
echo $quote->Symbol; 
echo '<br>'; 
echo 'Last Price: '; 
echo $quote->Last; 
echo '<br>'; 
echo 'Earnings To Price Ratio: '; 
echo $quote->P-E; 
echo '<br>'; 

Je sais que la seconde dernière ligne ($quote->P-E) est incorrect - je ne pense pas que vous pouvez utiliser des tirets comme ça. Mais pour une raison quelconque, je ne peux pas comprendre comment accéder à cette propriété. La chose étrange est que est comme il est écrit si je var_dump($quote) (C'est vers la fin):

object(SimpleXMLElement)#17 (16) { ["Symbol"]=> string(4) "AAPL" ["Last"]=> string(6) "271.87" ["Date"]=> string(9) "6/17/2010" ["Time"]=> string(6) "3:59pm" ["Change"]=> string(5) "+4.62" ["Open"]=> string(6) "270.72" ["High"]=> string(6) "272.90" ["Low"]=> string(6) "269.50" ["Volume"]=> string(8) "31195032" ["MktCap"]=> string(6) "247.4B" ["PreviousClose"]=> string(6) "267.25" ["PercentageChange"]=> string(6) "+1.73%" ["AnnRange"]=> string(15) "132.88 - 272.90" ["Earns"]=> string(6) "11.796" ["P-E"]=> string(5) "22.66" ["Name"]=> string(10) "Apple Inc." } 

Comment dois-je accèderont à cet attribut/propriété?

Répondre

4
$quote->{'P-E'}; 
Questions connexes