通过PHP评估SimpleXMLElement

时间:2016-04-05 09:10:21

标签: php simplexml

如果我打印出以下内容

$oXML = new \SimpleXMLElement($jobs);

我得到的数据是这样的

object(SimpleXMLElement)#359 (1) {
  ["Job"]=>
  array(4) {
    [0]=>
    object(SimpleXMLElement)#360 (10) {
      ["ID"]=>
      string(7) "J000001"
      ["Name"]=>
      string(13) "Internal Time"
      ["Description"]=>
      string(152) "Use this job to record your internal and non-billable time for activities such as annual leave, sick leave, professional development, staff meetings etc"
      ["Client"]=>
      object(SimpleXMLElement)#366 (2) {
        ["ID"]=>
        string(7) "8430219"
        ["Name"]=>
        string(11) "Fake Client"
      }
      ["ClientOrderNumber"]=>
      object(SimpleXMLElement)#367 (0) {
      }
      ["State"]=>
      string(7) "Planned"
      ["StartDate"]=>
      string(19) "2016-03-21T00:00:00"
      ["DueDate"]=>
      string(19) "2017-03-21T00:00:00"
      ["InternalID"]=>
      string(8) "11442733"
      ["Assigned"]=>
      object(SimpleXMLElement)#368 (1) {
        ["Staff"]=>
        object(SimpleXMLElement)#369 (2) {
          ["ID"]=>
          string(6) "344460"
          ["Name"]=>
          string(10) "Some Name"
        }
      }
    }
    [1]=>
    object(SimpleXMLElement)#361 (10) {
      ["ID"]=>
      string(7) "J000002"
      ["Name"]=>
      string(26) "Setup and Conversion Steps"
      ["Description"]=>
      object(SimpleXMLElement)#368 (0) {
      }
      ["Client"]=>
      object(SimpleXMLElement)#367 (2) {
        ["ID"]=>
        string(7) "8550368"
        ["Name"]=>
        string(4) "Some Client"
      }
      ["ClientOrderNumber"]=>
      object(SimpleXMLElement)#366 (0) {
      }
      ["State"]=>
      string(7) "Planned"
      ["StartDate"]=>
      string(19) "2016-03-24T00:00:00"
      ["DueDate"]=>
      string(19) "2016-04-07T00:00:00"
      ["InternalID"]=>
      string(8) "11442734"
      ["Assigned"]=>
      object(SimpleXMLElement)#369 (1) {
        ["Staff"]=>
        object(SimpleXMLElement)#370 (2) {
          ["ID"]=>
          string(6) "344460"
          ["Name"]=>
          string(10) "John Doe"
        }
      }
    }
    [2]=>
    object(SimpleXMLElement)#362 (10) {
      ["ID"]=>
      string(7) "J000004"
      ["Name"]=>
      string(9) "retertret"
      ["Description"]=>
      object(SimpleXMLElement)#369 (0) {
      }
      ["Client"]=>
      object(SimpleXMLElement)#366 (2) {
        ["ID"]=>
        string(7) "8430219"
        ["Name"]=>
        string(11) "Fake Client"
      }
      ["ClientOrderNumber"]=>
      object(SimpleXMLElement)#367 (0) {
      }
      ["State"]=>
      string(7) "Planned"
      ["StartDate"]=>
      string(19) "2016-04-01T00:00:00"
      ["DueDate"]=>
      string(19) "2016-04-01T00:00:00"
      ["InternalID"]=>
      string(8) "11612898"
      ["Assigned"]=>
      object(SimpleXMLElement)#368 (0) {
      }
    }
  }
}

所以有很多SimpleXMLElements,甚至是内部的。我基本上要做的是创建一个数组,其中ID作为键,名称作为值。 要做到这一点,我目前正在做

$jobsArray = array();
$i = 0;

foreach ($oXML->Jobs->Job as $job) {
    if($job->Client->Name->__toString() == $clientName->clientName) {
        $jobsArray[$i] = array(
            $job->ID->__toString() => $job->Name->__toString()
        );
        $i++;
    }
}

现在这似乎有效,但只有当我在所有内容上使用__toString时。出于某种原因,这样做并不正确。 有没有办法改进上面的内容,还是需要使用toString?

由于

1 个答案:

答案 0 :(得分:0)

你可以使用(字符串)($ job-> ID)=> (字符串)($求职>名称)。但是在使用XML / HTML对象时,您经常需要将其转换为...