XPATH - get single value returned instead of array php

◇◆丶佛笑我妖孽 提交于 2019-12-30 11:27:07

问题


I am using Xpath in PHP - I know that my query will return either 0 or 1 results.

If 1 result is returned I do not want it as an array - which is what is returned right now. I simply want the value without having to access the [0] element of the result and cast to a string.

Is this possible?


回答1:


Using 'evaluate' instead of 'query', you can do things like casting.

  • DOMXPath::evaluate()

Also, if you're just annoyed with doing stuff a lot of times, just write a function that does it ... that is the whole idea behind functions, right?




回答2:


If 1 result is returned I dont want it as an array - which is what is returned. I simply want the value without having to access the [0] element of the result and cast to a string.

That is possible with XPath's string function

A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

and DOMXPath's evaluate method:

Returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression.

Example:

$dom = new DOMDocument;
$dom->loadXML('<root foo="bar"/>');
$xp = new DOMXPath($dom);
var_dump($xp->evaluate('string(/root/@foo)')); // string(3) "bar"

If there was a built in xpath way of grabbing the first and only the first node value then that would be much more preferable over writing a function to do it

You can use the position function:

The position function returns a number equal to the context position from the expression evaluation context.

Example:

$dom = new DOMDocument;
$dom->loadXML('<root><foo xml:id="f1"/><foo xml:id="f2"/></root>');
$xp = new DOMXPath($dom);
var_dump($xp->evaluate('string(/root/foo[position() = 1]/@xml:id)')); // string(2) "f1"

or the abbreviated syntax

$dom = new DOMDocument;
$dom->loadXML('<root><foo xml:id="f1"/><foo xml:id="f2"/></root>');
$xp = new DOMXPath($dom);
var_dump($xp->evaluate('string(/root/foo[1]/@xml:id)')); // string(2) "f1"

Note that when querying for descendants with // using the position function might yield multiple result due to the way the expression is evaluated.




回答3:


probably

if ($array[0]){
    $string = $array[0];
}

?

if $array[0] is an array, you can rename string to new_array

if ($array[0]){
    $new_array  = $array[0];
}



回答4:


Your question suggests that you are using SimpleXML because you talk about an array. However long-time ago you accepted an answer giving an answer with DOMDocument. In any case other users go here looking for a solution in SimpleXML it works a little differently:

list($first) = $xml->xpath('//element') + array(NULL);

The element in $first if not NULL (for no elements) then still will be of type SimpleXMLElement (either an element node or an attribute node depending on the xpath query), however you can just cast it to string in PHP and done or you just use it in string context, like with echo:

echo $first;



回答5:


You can write it most simply like this:

$string = @$array[0];

The @ operator will suppress errors, making $string null if $array is empty.



来源:https://stackoverflow.com/questions/7464092/xpath-get-single-value-returned-instead-of-array-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!