Solved

PHP echo array key

Posted on 2010-09-17
2
314 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
Comment Utility
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
Comment Utility
works for me ;)

Thanks,

HNM
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now