Jump to content

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


Recommended Posts

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]=""

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.