simplexml_load_file : 迭代并分离具有属性的节点

simplexml_load_file : iterate and separate node with attribute

我完全不确定这个标题,很抱歉,这是我的问题:

我正在遍历 xml 并从中声明变量。它工作正常,看起来像这样:

$xml = simplexml_load_file('path/to/myxml.xml');
$bay0 = $xml->xpath("//Car[@park='0'  and contains(Group, '$filter')]");

foreach($bay0 as $vehicle) {
    $brand = $vehicle->Brand;
    $model = $vehicle->Model;
    $equips = $vehicle->Equipments
    [...]
 }

等等。

我的xml里面有这些车的装备。装备可以有属性 type="1"type="2" 像这样:

 <Equipments>
      <Equipment type="1">automatic doors</Equipment>
      <Equipment type="2">full led headlights</Equipment>
  </Equipments>

我希望能够在我的 foreach 中将设备分为两个不同的变量,具体取决于它们的类型,但我没有找到这个问题的答案,部分原因是我什至无法正确地表达我正在寻找的东西对于.

感谢您的帮助。

编辑:

添加我正在寻找的示例:

  foreach($bay0 as $vehicle) {
    $brand = $vehicle->Brand;
    $model = $vehicle->Model;
    $equips1 = $vehicle->Equipments[type="1"];
    $equips2 = $vehicle->Equipments[type="2"];
 }

我觉得可以帮到你

foreach($bay0 as $vehicle) {
    $brand = $vehicle->Brand;
    $model = $vehicle->Model;

    $equips=[];//start a array
    if($vehicle->Equipments[0]['type']=='1'){
        $equips[1] = $vehicle->Equipments;
    //$equips[1][] = $vehicle->Equipments; if many
    }
    elseif($vehicle->Equipments[0]['type']=='2'){
        $equips[2] = $vehicle->Equipments;
    //$equips[2][] = $vehicle->Equipments; if many
    }
[...]
 }