473,513 Members | 3,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multidimensional associative array

hi,
I have a table of prices that is dependent on the size of item, and
the number of that item.
something like this(i am sure the formating won't come out correctly)
............1".........1.25"
100.....$25.......$30
500.....$100.....$125
1000...$190.....$230
2500...$450.....$500

i want to do something like
priceArray['oneInch'][500] and get 100

I set up this array
var priceArray = [ {100:25, 500:100, 1000:190, 2500:450},
{100:30, 500:125, 1000:230, 2500:500}];

and can reference like this

priceArray[0][500] and get 100

but how can I setup a text key like

priceArray['oneInch'][500]
Thanks,
chuck
Sep 6 '05 #1
1 1671
chuck wrote:
hi,
I have a table of prices that is dependent on the size of item, and
the number of that item.
something like this(i am sure the formating won't come out correctly)
It didn't ;-)
...........1".........1.25"
100.....$25.......$30
500.....$100.....$125
1000...$190.....$230
2500...$450.....$500

i want to do something like
priceArray['oneInch'][500] and get 100

I set up this array
var priceArray = [ {100:25, 500:100, 1000:190, 2500:450},
{100:30, 500:125, 1000:230, 2500:500}];

and can reference like this

priceArray[0][500] and get 100

but how can I setup a text key like

priceArray['oneInch'][500]


Use an object, which means you loose the special features of an array
but likely you weren't using them anyway (though length may have been
handy). Declare your price object using either (I've changed the name):

var priceObj = {};
priceObj['1.0'] = {100:25, 500:100, 1000:190, 2500:450};
priceObj['1.5'] = {100:30, 500:125, 1000:230, 2500:500};

or

var priceObj = {
'1.0' : {100:25, 500:100, 1000:190, 2500:450},
'1.5' : {100:30, 500:125, 1000:230, 2500:500}
};

whichever is best for you (remember not to put a comma after the last
member of priceObj in the second method). The index can be any string
you like, I've used decimal numbers for convenience. I suppose you
could use words or phrases but that doesn't seem sensible to me:

var priceObj = {
'one inch' : {100:25, 500:100, 1000:190, 2500:450},
'one point five inches' : {100:30, 500:125, 1000:230, 2500:500}
};
You can access the values by:

alert( priceObj['1.0'][500] );
Since you don't have a length, you can't use a typical for loop like
for(... i<priceObj.length; ...) to loop through priceObj, so use for..in:

var prices = [ 'size - qty : price'];
var qty, s, sizes;
for ( s in priceObj ) {
sizes = priceObj[s]
for ( qty in sizes ) {
prices.push( s + '" - ' + qty + ': $' + sizes[qty] );
}
}
alert( prices.join('\n'));

--
Rob
Sep 6 '05 #2

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

Similar topics

2
14021
by: Mark | last post by:
Does Smarty support assigning multidimensional arrays? I've tried to get it working, but just can't get it. can someone help me? this is what I have in my php file: $side_bar = array('link' => array( 'index.php', 'estate.php', 'text' => array( 'Home',
5
6738
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my...
3
2129
by: jdemello | last post by:
When using a multidimensional associative array to cross-reference data in a second, single-dimensional array, PHP stops processing data after a few iterations. Memory usage according to the task manager doesn't seem to spike and CPU usage only gets as high as 11 to 13 percent. A sample script is provided below, any help or alternative...
21
18841
by: Leonardo | last post by:
I have the following associative array: $user=1; $user=1; $user=0; $user=1; $user=0; $user=1; $user=1;
2
8522
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo "$MyArray"; I get the following: Array
6
1892
by: Ant | last post by:
Hi, I'm trying to store some user input from a web form into an array, now the thing is I would like to store multiple users entries so I was thinking of using a multidimensional array so I might have something like this. userArray would hold user 0's UserID userArray would hold user 0's Name and userArray would hold user 1's UserID
8
7673
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using this: sType = "String"
3
1859
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I have a feeling that I need to do this through a multidimensional array. Am I on the right track? thanks, matt
41
4876
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
5
5372
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do something like this? var $userArray = array(array());
0
7269
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...
0
7177
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...
0
7394
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. ...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
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...
0
3248
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...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
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
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.