Fatal error:call to undefined method stdClass::xpath()

939 views Asked by At

I have this XML:

<JobReference> <Type>STANDARD</Type> <Title>N° ANCIEN DOSSIER</Title> <Reference/>

And am running this PHP code:

$xmljobreference = simplexml_load_string ($GetJobResult->JobReferences->JobReference->Title );    
$referenceanciendossier = $GetJobResult->JobReferences->xpath( "//JobReference[@Title ='N°  ANCIEN DOSSIER']/Reference" );

But I am getting this error:

Fatal error:call to undefined method stdClass::xpath() on line 190

If I run var_dump($GetJobResult) I get:

 public 'JobReferences' => 
    object(stdClass)[149]
      public 'JobReference' => 
        array (size=7)
          0 => 
            object(stdClass)[150]
              ...
          1 => 
            object(stdClass)[151]
              ...
          2 => 
            object(stdClass)[152]

             public 'JobSeq' => int 920179
1

There are 1 answers

2
Gustaf Gunér On

Well, as your var_dump shows you, JobReferences is built up by a JobReference which itself is an array with 7 items.

So assuming that you want to run the xpath function on all the array items, you need to run a foreach loop. Something like this should work:

$array = $GetJobResult->JobReferences->JobReference;
$results = array();

foreach($array as $reference){
    array_push($results, $reference->xpath( "//JobReference[@Title ='N°  ANCIEN DOSSIER']/Reference" ));
}
var_dump($results);