Jim from Oakland Posted July 4, 2006 Share Posted July 4, 2006 Array named $accountData is structured as shown below.How do I loop all entries in the first dimension (there are 3 in the example) to access a specific 2nd dimension key within each (such as the Value key in the example)?I assume I need some form of foreach but I can't figure it out.[UserID][Label]="User Identifier"[UserID][Value]="Q2ED"[UserID][Help]="This is yourprimary ID #"[UserID][Note]="First entered March 2003"[UserType][Label]="User Type"[UserType][Value]="Gold"[UserType][Help]="This refers to your account category"[UserType][Note]="Upgrade is pending." [UserName][Label]="User Name"[UserName][Value]="Bill Derr"[UserName][Help]="We must have your name."[UserName][Note]="" Quote Link to comment https://forums.phpfreaks.com/topic/13607-array-loop-1st-dimension-keys-access-a-2nd-dimension-key/ Share on other sites More sharing options...
kenrbnsn Posted July 4, 2006 Share Posted July 4, 2006 Here's an example of nested foreach loops which print the information in your array:[code]<?php$accountData['UserID']['Label']="User Identifier";$accountData['UserID']['Value']="Q2ED";$accountData['UserID']['Help']="This is yourprimary ID #";$accountData['UserID']['Note']="First entered March 2003";$accountData['UserType']['Label']="User Type";$accountData['UserType']['Value']="Gold";$accountData['UserType']['Help']="This refers to your account category";$accountData['UserType']['Note']="Upgrade is pending."; $accountData['UserName']['Label']="User Name";$accountData['UserName']['Value']="Bill Derr";$accountData['UserName']['Help']="We must have your name.";$accountData['UserName']['Note']="";foreach($accountData as $fl => $arry) { echo '<span style="text-decoration:underline">' . $fl . '</span><br>'; foreach ($arry as $k=>$v) if ($v != '') echo '<span style="font-weight:bold;color:red">' . $k . ':</span> ' . $v . '<br>'; echo "<br>\n"; }?>[/code]Note: I put in some styling so you can see the output better. :)Ken Quote Link to comment https://forums.phpfreaks.com/topic/13607-array-loop-1st-dimension-keys-access-a-2nd-dimension-key/#findComment-52731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.