php SimpleXML check if a child exists

后端 未结 16 2141
粉色の甜心
粉色の甜心 2020-11-27 06:24

A->b->c might exist but c might not exist. How do I check it?

16条回答
  •  [愿得一人]
    2020-11-27 07:09

    I solved it by using the children() function and doing a count() on it, ignoring an PHP error if there are no children by putting an @ before the count-call. This is stupid, but it works:

    $identification = $xml->identification;
    if (@count($identification->children()) == 0)
      $identification = $xml->Identification;
    

    I hate this...

提交回复
热议问题