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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.