我有这个SimpleXmlElement:
SimpleXMLElement {#625
+"productCategoryAttribute": array:4 [
0 => "Yes"
1 => "No"
2 => "Maybe"
3 => "Yes"
]
}
我明白了:
$xml = new SimpleXMLElement(Storage::get($path));
这是原始的xml:
<productCategoryAttributes>
<productCategoryAttribute key="long">Yes</productCategoryAttribute>
<productCategoryAttribute key="short">No</productCategoryAttribute>
<productCategoryAttribute key="long">Maybe</productCategoryAttribute>
<productCategoryAttribute key="short">Yes</productCategoryAttribute>
</productCategoryAttributes>
我的问题是我如何获得钥匙?
答案 0 :(得分:2)
您需要使用SimpleXMLElement::attributes()
:
foreach($productCategoryAttributes as $element) {
$attrs = $element->attributes();
echo 'key = ' . $attrs['key'];
echo 'value = ' . $element;
}
答案 1 :(得分:2)
访问特定属性的最简单方法是使用数组索引 - 在这种情况下为['key']
:
foreach ($xml->productCategoryAttribute as $attribute) {
echo (string) $attribute['key'], ' = ', (string) $attribute, PHP_EOL;
}
long =是
短=没有
long =也许
短=是