473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Displaying an array with its keynames

I have an array with string and numerical indices:
$arr ['x'][0] = 10;
$arr ['y'][1] = 90;
$arr ['d'][1] = 45;
$arr ['y'][0] = 34;

Keys are not in a sorted order and preferably it stays that way. How can I
display the results like this:
x0 : 10
y1: 90
d1: 45
y0: 34

,using a foreach loop ? I tried with array_keys () like so:
foreach ($arr as $a) {
echo array_keys($a)." $a <br>";
}

but my output is this:
Array 10
Array 90
Array 45
Array 34

Is a foreach just not possible here or do I need to use another function or
this one differently ?
Can you help ? Thanks in advance!
Pjotr
Jul 17 '05 #1
3 1379
On Fri, 23 Jul 2004 19:03:22 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote:
I have an array with string and numerical indices:
$arr ['x'][0] = 10;
$arr ['y'][1] = 90;
$arr ['d'][1] = 45;
$arr ['y'][0] = 34;
[snip]
,using a foreach loop ? I tried with array_keys () like so:
foreach ($arr as $a) {
echo array_keys($a)." $a <br>";
}


Hmm, a foreach inside a foreach should do it.
foreach ($arr as $key => $val) {
foreach ($val $as $k => $v) {
print "$key: $k: $v<br />";
}
}

Best regards,
Madsen

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBAUlAlNHJe/JASHcRArieAJ40nqjA6iiDp/AuFqy2EfxkSsQhaQCdFiL0
VAnpSlFlmGfTBB1Wnu9V2BA=
=3iB3
-----END PGP SIGNATURE-----

Jul 17 '05 #2
On Fri, 23 Jul 2004 19:03:22 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote:
I have an array with string and numerical indices:
$arr ['x'][0] = 10;
$arr ['y'][1] = 90;
$arr ['d'][1] = 45;
$arr ['y'][0] = 34;
[snip]
,using a foreach loop ? I tried with array_keys () like so:
foreach ($arr as $a) {
echo array_keys($a)." $a <br>";
}


Hmm, a foreach inside a foreach should do it.
foreach ($arr as $key => $val) {
foreach ($val $as $k => $v) {
print "$key: $k: $v<br />";
}
}

Best regards,
Madsen

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."
It took me a while before I found your attached reply, but it works like a
charm, thanks a bunch!
Pjotr
Jul 17 '05 #3
"Pjotr Wedersteers" wrote:
I have an array with string and numerical indices:
$arr [’x’][0] = 10;
$arr [’y’][1] = 90;
$arr [’d’][1] = 45;
$arr [’y’][0] = 34;

Keys are not in a sorted order and preferably it stays that way. How can I
display the results like this:
x0 : 10
y1: 90
d1: 45
y0: 34

,using a foreach loop ? I tried with array_keys () like so:
foreach ($arr as $a) {
echo array_keys($a)." $a <br>";
}

but my output is this:
Array 10
Array 90
Array 45
Array 34

Is a foreach just not possible here or do I need to use another
function or
this one differently ?
Can you help ? Thanks in advance!
Pjotr


copied from php manual for "foreach"

/* foreach example 4: multi-dimensional arrays */

$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Displayi...ict132752.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=444047
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JW | last post by:
Dear NG, Still working on my .NET CF C# forms application ... I have an array of forms that I wish to display in array order, and thus have created a loop to run through them. Due to...
4
by: MrL8Knight | last post by:
Hello, I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to...
2
by: james | last post by:
I wrote values to the registry in a "MultiString" registry type. I now want to retrieve these into a string array. I use this: String LoadedObject = SUBKEY.GetValue(KeyNames.ToString()) but...
13
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use...
1
by: mandakini | last post by:
Hello freinds I am working on this url http://72.36.156.243/compbuild.php Here I am using ifram and displaying dynamic value I don't know how to use iframe as array how to assign array and...
1
by: seadog | last post by:
Hi, I am having a problem displaying data in a textbox array. What I am trying to do is have 5 number enter 5 textboxes, starting from the first and working there way to the bottom textbox. What...
3
by: comatose28 | last post by:
Hello all, I am having a problem displaying a txt file I have. I have code to parse it but I do not know how to display the information. The code I have is: <?php // Process server data if (...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.