Solved

PHP echo array key

Posted on 2010-09-17
2
317 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

828 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