【问题标题】:How to access a specific key from an associative array in PHP?如何从 PHP 中的关联数组访问特定键?
【发布时间】:2013-09-27 10:12:33
【问题描述】:

我是 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 中访问或引用这个键吗?提前致谢。

【问题讨论】:

  • 我想你想要:$sample['children']['types'][0]['children']['xsd:schema'] ?

标签: php arrays multidimensional-array associative-array


【解决方案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 之外),那么您将需要编写递归函数或循环来查找它。

【讨论】:

  • 其实我放了一小部分数组。该数组比我上面粘贴的代码大得多。如果我想访问键为 xsd:schema 的所有数组元素,我该怎么办?我应该使用 foreach 还是 wt?你能指导我吗?谢谢你的回答。
  • 是的,为了访问所有称为 xsd:schema 的元素,您必须遍历数组。我会更新我的答案以反映这一点。
【解决方案2】:

我猜你的目标是寻找键为“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";
}

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

【讨论】:

  • 另外,当首先创建数组时,请确保使用“somekey”而不仅仅是 somekey 作为键。它必须是一个字符串。如果您忘记了这一点,它看起来仍然可以工作,但它可能无法在我上面建议的代码中运行。
猜你喜欢
  • 1970-01-01
  • 2013-07-08
  • 2013-05-02
  • 2021-03-16
  • 2016-08-13
  • 2020-05-26
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
相关资源
最近更新 更多