Jump to content


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

Jim from Oakland

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][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."

Share this post

Link to post
Share on other sites
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. :)


Share this post

Link to post
Share on other sites


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.