我是PHP中这个关联数组概念的新手。现在我有一个名为$sample
的数组如下:
Array
(
[name] => definitions
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_Server_Reporting
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[types] => Array
(
[0] => Array
(
[name] => types
[text] =>
[attributes] => Array
(
)
[children] => Array
(
[xsd:schema] => Array
(
[0] => Array
(
[name] => schema
[text] =>
[attributes] => Array
(
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[xsd:complextype] => Array
(
[0] => Array
(
[name] => complextype
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_FaultMessage
)
[children] => Array
(
[xsd:sequence] => Array
(
[0] => Array
(
[name] => sequence
[text] =>
[attributes] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
从上面的数组我想引用(或访问)键 xsd:schema 。但是我无法做到。你能否告诉我如何从关联数组名$sample
访问或引用此键?提前谢谢。
答案 0 :(得分:1)
要访问此值,您可以使用: -
$sample['children']['types'][0]['children']['xsd:schema'];
如果您的types
数组中有多个这些元素,则需要遍历它们: -
foreach($sample['children']['types'] as $type) {
if(isset($type['children']) && isset($type['children']['xsd:schema'])) {
// Perform action on element
$type['children']['xsd:schema'];
}
}
如果您不知道您的结构(如在xsd:schema中可能出现在types
之外),那么您将需要编写一个递归函数或循环来查找它。
答案 1 :(得分:0)
我猜你的目标是寻找关键是" xsd"的键/值对。 ?
如果是这样,在PHP中,您可以使用以下逻辑来实现:
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
// OR
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
只需添加一组递归或嵌套循环来遍历结构,直到找到正确的密钥。