Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check a value of simplexml object

i have an array that is simple xml object. i am writing the codes below

Array
(
[ID] => 1992109
[Title] => A Equipa do MAIS
[Description] => SimpleXMLElement Object
    (
    )
 )

now how can i check the value of Description in this array that if there is a value of Description is present or not.

like image 200
Manish Jangir Avatar asked Feb 22 '23 11:02

Manish Jangir


1 Answers

To verify if a SimpleXMLElement object has a text value, you'll need to cast it as a string:

$desc = (string)$array['Description'];
if (!empty($desc)) {
  echo $desc;
}

Although you are able to directly echo the contents of a SimpleXMLElement object, to use its string value as a variable requires typecasting it. empty() must act on a variable1, so the element's implicit __toString() call won't work as it does with echo.


1Beginning with PHP 5.5, empty() can test an arbitrary expression's result. It no longer requires a variable as its argument.

like image 112
Michael Berkowski Avatar answered Mar 08 '23 13:03

Michael Berkowski