duanlong4890 2013-09-27 10:12
浏览 22
已采纳

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

I'm a newbie to this associative array concept in PHP. Now I'm having an array named $sample as follows:

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. 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? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douduan5073 2013-09-27 10:17
    关注

    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:-

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line