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

Php 如何从名称而不是索引中获取数组值?,php,xml,simplexml,Php,Xml,Simplexml,我有以下xml: <result> <rowset name="jumpClones" key="jumpCloneID" columns="jumpCloneID,typeID,locationID,cloneName"/> <rowset name="jumpCloneImplants" key="jumpCloneID" columns="jumpCloneID,typeID,typeName"/> <rowset name="implants"

我有以下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>
现在,行集[2]有:name=“implants”

那么,如何使用数组索引属性名称(
imports
)来获取值,而不是指向索引
2

我试过:

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

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

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

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

这不是我需要的=(我需要更改行集[2]对于名称:imports。因为如果我的xml更改并获得更多行集,索引也会更改。因此我确实需要从属性名称而不是索引中获取值。@user3790692哦,好的,那么您希望能够通过
imports
而不是索引来调用它们吗?正确吗?@user3790692当然很高兴这有帮助
$implants = $array->xpath('//rowset[@name="implants"]');
if(!empty($implants)) {
    echo $implants[0]->row[0]->attributes()->typeName;
}