Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Array to String conversion notice when getting $variable value with xpath

Im trying to get the name atriburte of the type element in the following feed using xpath.

<response request="getHierarchyByMarketType" code="001" 
          message="success" debug="">    
<jonny>    
  <class id="5" name="Formula 1" maxRepDate="2012-12-19" maxRepTime="15:03:34">    
    <type id="4558" name="F1 Championship" lastUpdateDate="2012-11-26"
          lastUpdateTime="16:17:33">

to do this I'm using

$market_name = $wh_xml->xpath('/response/jonny/class/type/@name');

and then using

<h2>
  <?= $market_name ?>
</h2>

in my view, but instead of it returning my expected "F1 Championship" im getting a:

Array to string conversion

notice, but I'm not sure why, I thought xpath would return the value of @name as a string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT: use list() to get just one value instead of an array:

list($market_name) = $wh_xml->xpath('//type/@name');
echo $market_name;

see it working: http://3v4l.org/rNdMo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...