简体   繁体   English

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

[英]How to access a specific key from an associative array in PHP?

I'm a newbie to this associative array concept in PHP. 我是PHP中此关联数组概念的新手。 Now I'm having an array named $sample as follows: 现在,我有一个名为$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
                          (
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )
)

From the above array I want to refer(or access) to the key xsd:schema . 从上面的数组中,我想引用(或访问)键xsd:schema But I'm not able to do it. 但是我做不到。 Can you please tell me how should I access or refer this key from the associative array names $sample ? 您能告诉我如何从关联数组名称$sample访问或引用此键吗? Thanks in advance. 提前致谢。

To access this value you would use:- 要访问此值,您可以使用:-

$sample['children']['types'][0]['children']['xsd:schema'];

If you have multiple of these elements in your types array you will need to loop through them:- 如果您的types数组中有多个这些元素,则需要遍历它们:-

foreach($sample['children']['types'] as $type) {
   if(isset($type['children']) && isset($type['children']['xsd:schema'])) {

       // Perform action on element
       $type['children']['xsd:schema'];

   }
}

If you do not know your structure (as in xsd:schema can occur outside of types ) then you will need to write a recursive function or loop for finding it. 如果您不知道自己的结构(例如xsd:schema可能发生在types之外),则需要编写一个递归函数或循环来查找它。

I guess your goal is to seek for the key/value pair where the key is "xsd" ? 我想您的目标是寻找键为“ xsd”的键/值对吗?

If so, in PHP, you can do so by using the follwing logic: 如果是这样,在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";
}

Just add a set of recursing or nested loops to traverse the structure until you find the proper key. 只需添加一组递归或嵌套循环即可遍历结构,直到找到正确的键为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM