简体   繁体   English

遍历PHP中的数组和子集

[英]Loop through arrays and subsets in PHP

I have the following array: 我有以下数组:

Array ( [@attributes] => Array ( [version] => 010 [release] => 006 ) [Header] => Array ( [To] => 6891118750001 [From] => 9911557 [MessageID] => 306b197fff044421b31c0a2a15e356c1 [RelatesToMessageID] => Resolution [SentTime] => 2014-09-16T12:19:50.8Z [Security] => Array ( [Sender] => Array ( [TertiaryIdentification] => 165 ) [Receiver] => Array ( [TertiaryIdentification] => 2341 ) ) [RxReferenceNumber] => RxRef# REFREQ 2.1 [PrescriberOrderNumber] => 13 ) [Body] => Array ( [RefillRequest] => Array ( [Pharmacy] => Array ( [Identification] => Array ( [NCPDPID] => 9911557 [NPI] => 1801849179 ) [StoreName] => CA Pharmacy 10.6MU [Address] => Array ( [AddressLine1] => 65432 Cabernet Turn [City] => Sonoma [State] => CA [ZipCode] => 95476 ) [CommunicationNumbers] => Array ( [Communication] => Array ( [Number] => 7075557071 [Qualifier] => TE ) ) ) [Prescriber] => Array ( [Identification] => Array ( [NPI] => 1234567893 ) [Name] => Array ( [LastName] => Jefferson ) [Address] => Array ( [AddressLine1 Array([@attributes] => Array([version] => 010 [release] => 006)[Header] => Array([To] => 6891118750001 [From] => 9911557 [MessageID] => 306b197fff044421b31c0a2a15e356c1 [RelatesToMessageID ] =>分辨率[SentTime] => 2014-09-16T12:19:50.8Z [Security] => Array([Sender] => Array([TertiaryIdentification] => 165)[Receiver] => Array([TertiaryIdentification] => 2341))[RxReferenceNumber] => RxRef#REFREQ 2.1 [PrescriberOrderNumber] => 13)[Body] => Array([RefillRequest] => Array([Pharmacy] => Array([Identification] => Array([ NCPDPID] => 9911557 [NPI] => 1801849179)[StoreName] => CA Pharmacy 10.6MU [Address] => Array([AddressLine1] => 65432 Cabernet Turn [City] => Sonoma [State] => CA [ZipCode ] => 95476)[CommunicationNumbers] => Array([Communication] => Array([Number] => 7075557071 [Qualifier] => TE))))[Prescriber] => Array([Identification] => Array([NPI ] => 1234567893)[名称] =>数组([LastName] => Jefferson)[地址] =>数组([AddressLine1 ] => 5555 Labelle Lane [City] => Gaithersburg [State] => MD [ZipCode] => 20877 ) [CommunicationNumbers] => Array ( [Communication] => Array ( [0] => Array ( [Number] => 3105551515 [Qualifier] => TE ) [1] => Array ( [Number] => 3015551516 [Qualifier] => FX ) ) ) ) [Patient] => Array ( [Name] => Array ( [LastName] => Li [FirstName] => Ci ) [Gender] => U [DateOfBirth] => Array ( [Date] => 1923-10-18 ) ) [MedicationPrescribed] => Array ( [DrugDescription] => ZIOPTAN 0.0015% Ophthalmic Solution [DrugCoded] => Array ( [ProductCode] => 00006393130 [ProductCodeQualifier] => ND ) [Quantity] => Array ( [Value] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] => C54702 ) [Directions] => D [Substitutions] => 0 [WrittenDate] => Array ( [Date] => 2014-04-01 ) ) [MedicationDispensed] => Array ( [DrugDescription] => ZIOPTAN 0.0015% Ophthalmic Solution [DrugCoded] => Array ( [ProductCode] => 00006393130 [ProductCodeQualifier] => ND [DrugDBCode] => 1244616 [DrugDBCodeQualifier] => ] => 5555 Labelle Lane [City] => Gaithersburg [State] => MD [ZipCode] => 20877)[CommunicationNumbers] => Array([Communication] => Array([0] => Array([Number] = > 3105551515 [Qualifier] => TE)[1] => Array([Number] => 3015551516 [Qualifier] => FX)))))[Patient] => Array([Name] => Array([LastName] = > Li [名字] => Ci)[性别] => U [DateOfBirth] =>数组([Date] => 1923-10-18))[MedicationPrescribed] =>数组([DrugDescription] => ZIOPTAN 0.0015%Ophthalmic解决方案[DrugCoded] =>数组([ProductCode] => 00006393130 [ProductCodeQualifier] => ND)[Quantity] => Array([Value] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] = > C54702)[方向] => D [替代] => 0 [书面日期] =>阵列([日期] => 2014-04-01))[已分配药物] =>阵列([药物描述] => ZIOPTAN 0.0015%眼药解决方案[DrugCoded] =>数组([ProductCode] => 00006393130 [ProductCodeQualifier] => ND [DrugDBCode] => 1244616 [DrugDBCodeQualifier] => SBD ) [Quantity] => Array ( [Value] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] => C54702 ) [Directions] => D [Substitutions] => 0 [WrittenDate] => Array ( [Date] => 2014-04-01 ) [LastFillDate] => Array ( [Date] => 2014-04-02 ) ) ) ) ) version = 010 SBD)[Quantity] => Array([Value] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] => C54702)[Directions] => D [Substitutions] => 0 [WrittenDate] = > Array([Date] => 2014-04-01)[LastFillDate] => Array([Date] => 2014-04-02))))))版本= 010

but when I loop through it with this code: 但是当我使用以下代码遍历它时:

function displayArrayRecursively($arr, $indent='') {
    if ($arr) {
        foreach ($arr as $value) {

            if (is_array($value)) {

                displayArrayRecursively($value, $indent . '');
            } else {
                echo key($arr) . " = $value<br>";
            }
        }
    }
}

displayArrayRecursively($arrXml);

It prints all the values but not all the correct subset key names are associated. 它打印所有值,但不是所有正确的子集键名都被关联。 Any ideas? 有任何想法吗?

Thanks! 谢谢!

NCoder NCoder

Don't use the key() function but access your array key directly with the variable $key (or whatever name) of foreach() . 不要使用key()函数,而是使用foreach()的变量$key (或任何名称)直接访问数组键。 Also, you weren't even using the variable $indent , so I added the piece of code you needed. 另外,您甚至没有使用$indent变量,因此我添加了所需的代码。 This is the function you are looking for: 这是您要寻找的功能:

function displayArrayRecursively($arr, $indent='')
{
    if ($arr)
        foreach ($arr as $key => $value)
            if (is_array($value))
                displayArrayRecursively($value, $indent.'--');
            else
                echo $indent.$key." = $value<br>";
}

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

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