Solved

PHP echo array key

Posted on 2010-09-17
2
316 Views
Last Modified: 2012-05-10
Hi Experts,

How can I echo the key of a key/value pair in an array.  For example:

array([part123]=>Available [part432]=>NotAvailable [part789]=>BackOrdered)

Let's say I want to echo in HTML both the key and the value, in this case the part number and the status but in different table <td>'s.

I can do the echoing and array handling.. but I can't remember how to specifically echo the key.

Any ideas?

Thanks,

HNM
0
Comment
Question by:HelpNearMe
2 Comments
 
LVL 11

Accepted Solution

by:
VanHackman earned 500 total points
ID: 33705285
like this:
<?php

$a = array(
    "uno" => 1,
    "dos" => 2,
    "tres" => 3,
    "diecisiete" => 17
);

echo '<table border="1">';

foreach ($a as $key => $value)
{
	echo "<tr>";
    echo "\t<td>$a[$key]</td> <td>$value.</td>\n";
	echo "</tr>";
}

echo "</table>";

?>

Open in new window

0
 

Author Closing Comment

by:HelpNearMe
ID: 33705302
works for me ;)

Thanks,

HNM
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question