如何在PHP中从关联数组访问特定键?

如何在PHP中从关联数组访问特定键?,php,arrays,multidimensional-array,associative-array,Php,Arrays,Multidimensional Array,Associative Array,我是PHP中这个关联数组概念的新手。现在我有一个名为$sample的数组,如下所示: Array ( [name] => definitions [text] => [attributes] => Array ( [name] => Mediation_Soap_Server_Reporting [targetnamespace] => https://mediation.moceanmobile.net/soap/reportin

我是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'];
如果在
类型
数组中有多个这样的元素,则需要对它们进行循环:-

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可以出现在
类型之外),那么您需要编写一个递归函数或循环来查找它。

我猜您的目标是查找键为“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";
}
while(列表($key,$value)=每个($arr)){
echo“Key:$Key;Value:$Value
\n”; } //或 foreach($arr作为$key=>$value){ echo“Key:$Key;Value:$Value
\n”; }

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

我想您需要:
$sample['children']['types'][0]['children']['xsd:schema']
?实际上我已经放置了数组的一小部分。该数组比我上面粘贴的代码大得多。如果我想访问键为xsd:schema的所有数组元素,我应该怎么做?我应该用foreach还是wht?你能在这方面指导我吗?谢谢你的回答。是的,为了访问所有名为xsd:schema的元素,你必须在数组中循环。我将更新我的答案以反映这一点。此外,当首先创建数组时,请确保使用“somekey”,而不仅仅是somekey作为键。它一定是一根绳子。如果您忘记了这一点,它看起来仍在工作,但可能无法在我上面建议的代码中运行。