Jump to content


array: loop 1st dimension keys, access a 2nd dimension key

  • Please log in to reply
1 reply to this topic

#1 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 04 July 2006 - 01:54 AM

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][Help]="This is yourprimary ID #"
[UserID][Note]="First entered March 2003"

[UserType][Label]="User Type"
[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."

#2 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 July 2006 - 02:30 AM

Here's an example of nested foreach loops which print the information in your array:
$accountData['UserID']['Label']="User Identifier";
$accountData['UserID']['Help']="This is yourprimary ID #";
$accountData['UserID']['Note']="First entered March 2003";

$accountData['UserType']['Label']="User Type";
$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.";

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";

Note: I put in some styling so you can see the output better. :)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users