473,791 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1401
On Fri, 23 Jul 2004 19:03:22 +0200
"Pjotr Wedersteers" <x3****@westert erp.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)

iD8DBQFBAUlAlNH Je/JASHcRArieAJ40n qjA6iiDp/AuFqy2EfxkSsQha QCdFiL0
VAnpSlFlmGfTBB1 Wnu9V2BA=
=3iB3
-----END PGP SIGNATURE-----

Jul 17 '05 #2
On Fri, 23 Jul 2004 19:03:22 +0200
"Pjotr Wedersteers" <x3****@westert erp.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
293
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 functional and speed reasons (this app. will be deployed to a PocketPC) I want to merely Hide() each form when the user is finished with it, rather than allow it to Close(). In this way the user can "back" button their way back to each previous form...
4
7159
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 serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see...
2
22240
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 this gives an error "Cannot implicitly convert type 'object' to 'string'. Which is fine, but what do I do? Googling, I saw this:
13
3883
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 the modified preorder tree transversal algorithm. What I wanted was on the first page is to display the catogories as follows Books (35) Electronics(23) The number inside the parenthesis being the number of products in that
1
2602
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 retrive value from array. I am very poore in javascript please help me. coding is like this
1
2479
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 is happening is that the displaying of the data is not happening until the process is complete, I have tryed a for next loop, for x = 1 to 5, text1(x).text = char(5,1), next x, I have even tryed just text(0).text = char(5,1), sleep 500, text(1).text =...
3
1318
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 ( $fdsStatusTXT = file_get_contents ( "fdsstatus.txt" ) ) { $fdsStatusArray = explode ( "\n", $fdsStatusTXT ); // Create gameData array $gameData = array (
1
4224
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 aligned to the top, along with the slideshow and link buttons, you have to scroll down to see the text - how can I make IE6 display correctly? http://geekarama.co.uk/new_home.html here is the code for new_home.html and following that the CSS...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.