如何从名称而不是索引中获取数组值


How to get array value from name instead index?

我有以下xml:

<result>
<rowset name="jumpClones" key="jumpCloneID" columns="jumpCloneID,typeID,locationID,cloneName"/>
<rowset name="jumpCloneImplants" key="jumpCloneID" columns="jumpCloneID,typeID,typeName"/>
<rowset name="implants" key="typeID" columns="typeID,typeName">
<row typeID="9899" typeName="Ocular Filter - Basic"/>
<row typeID="9941" typeName="Memory Augmentation - Basic"/>
<row typeID="9942" typeName="Neural Boost - Basic"/>
<row typeID="9943" typeName="Cybernetic Subprocessor - Basic"/>
<row typeID="9956" typeName="Social Adaptation Chip - Basic"/>
</rowset>
</result>

为了获得值Ocular Filter-Basic,我使用/通过rowset的索引:

$array->result->rowset[2]->row[0]["typeName"];
                   //  ^

现在,行集[2]有:name="implants"

那么,如何获取数组索引属性名称(implants)而不是指向索引2的值呢?

我试过:

$array->result->rowset["implants"]->row[0]["typeName"];

但它没有起作用。正确的解决方案是什么?

如果您想使用implants作为获取该节点的基础,而不是选择索引(这可能会有所不同),您可以使用xpath查询,如果找到指向它的查询,则访问它的子查询。示例:

$implants = $array->xpath('//rowset[@name="implants"]');
if(!empty($implants)) {
    echo $implants[0]->row[0]->attributes()->typeName;
}

样本输出