我有以下数组:
数组([@attributes] =>数组([版本] => 010 [发布] => 006) [标题] =>数组([To] => 6891118750001 [From] => 9911557 [MessageID] => 306b197fff044421b31c0a2a15e356c1 [RelatesToMessageID] =>分辨率[SentTime] => 2014-09-16T12:19:50.8Z [安全] => Array([Sender] => Array([TertiaryIdentification] => 165)[Receiver] => 数组([TertiaryIdentification] => 2341))[RxReferenceNumber] => RxRef#REFREQ 2.1 [PrescriberOrderNumber] => 13)[Body] =>阵列( [RefillRequest] =>数组([Pharmacy] =>数组([Identification] => 数组([NCPDPID] => 9911557 [NPI] => 1801849179)[StoreName] => CA 药房10.6MU [地址] =>数组([AddressLine1] => 65432赤霞珠 转[城市] =>索诺玛[州] => CA [ZipCode] => 95476) [CommunicationNumbers] =>数组([Communication] =>数组([Number] => 7075557071 [资格] => TE)))[Prescriber] =>数组([Identification] =>数组([NPI] => 1234567893)[名称] =>数组( [LastName] =>杰斐逊)[地址] =>数组([AddressLine1] => 5555 Labelle Lane [City] =>盖瑟斯堡[州] => MD [ZipCode] => 20877) [CommunicationNumbers] =>数组([Communication] =>数组([0] => 数组([Number] => 3105551515 [Qualifier] => TE)[1] =>阵列( [编号] => 3015551516 [资格] => FX))))[患者] =>阵列( [姓名] =>数组([LastName] => Li [FirstName] => Ci)[性别] => ü [DateOfBirth] =>数组([Date] => 1923-10-18)) [MedicationPrescribed] =>数组([DrugDescription] => ZIOPTAN 0.0015% 眼科溶液[DrugCoded] =>数组([ProductCode] => 00006393130 [ProductCodeQualifier] => ND)[数量] =>阵列( [值] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] => C54702)[方向] => D [Substitutions] => 0 [WrittenDate] =>数组([日期] => 2014-04-01)) [MedicationDispensed] =>数组([DrugDescription] => ZIOPTAN 0.0015% 眼科溶液[DrugCoded] =>数组([ProductCode] => 00006393130 [ProductCodeQualifier] => ND [DrugDBCode] => 1244616 [DrugDBCodeQualifier] => SBD)[数量] =>数组([Value] => 1 [CodeListQualifier] => 38 [UnitSourceCode] => AC [PotencyUnitCode] => C54702)[方向] => D [Substitutions] => 0 [WrittenDate] =>排列 ([Date] => 2014-04-01)[LastFillDate] =>数组([Date] => 2014-04-02)))))版本= 010
但是当我用这段代码循环它时:
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);
它会打印所有值,但不会关联所有正确的子集键名称。有什么想法吗?
谢谢!
NCoder
答案 0 :(得分:1)
不要使用key()
函数,而是直接使用$key
的变量foreach()
(或任何名称)访问数组键。此外,你甚至没有使用变量$indent
,所以我添加了你需要的代码片段。
这是您正在寻找的功能:
function displayArrayRecursively($arr, $indent='')
{
if ($arr)
foreach ($arr as $key => $value)
if (is_array($value))
displayArrayRecursively($value, $indent.'--');
else
echo $indent.$key." = $value<br>";
}