0

我是 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吗?提前致谢。

4

2 回答 2

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),那么您将需要编写一个递归函数或循环来查找它。

于 2013-09-27T10:17:22.740 回答
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";
}

只需添加一组递归或嵌套循环来遍历结构,直到找到正确的键。

于 2013-09-27T10:25:16.413 回答