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

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,我 using/through rowset 的索引:

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

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

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

我试过:

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

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

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

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

Sample Output