Jump to content

Multidimensional array


Omzy

Recommended Posts

I have an array as such:

 

$types=array(
'basic'=>array('Basic Package', '3', '140'),
'standard'=>array('Standard Package', '3', '140'),
'premium'=>array('Premium Package', '5', '200'),
);

 

In my database the 'type' field has been stored as a number, so 1=basic, 2=standard and 3=premium

 

How can I refer to the correct item in the array and output the first item in that array? Ideally I don't want to change the entries in the database...

 

The 'type' field can be referred to as $type

Link to comment
Share on other sites

Just create a new array that maps the integer to the string key.

 

$typeMap = array(1 => 'basic', 2 => 'standard', 3 => 'premium');

 

You can then refer to the type as $types[$typeMap[$type]]].

 

Come on, where are all you php freaks today lol.

 

Probably to the same place as your patience.

Link to comment
Share on other sites

How do you "know" what the individual positions are? You have created the rule that 1=basic, 2=standard and 3=premium. However, there is no reference in the $types array to its numerical equivalent. Unless you somehow tell the interpreter those rules, there is no way it can magically infer these or fetch them from your mind.

 

for example 'basic' would be position 0

 

No, it is not. Your array is associative, not enumerated.

Link to comment
Share on other sites

Well, like it or not, the computer cannot read your mind. If you don't tell it what to do it just won't know it. It doesn't just know something. There is no inherent relationship between the integer 1 and the string 'basic'.

 

I showed you how you would tell PHP your rules. You asked if it was possible to just know it. I answered "no" and told you the reason why based on how programming works. I don't see how that makes me an "ass".

Link to comment
Share on other sites

Crikey, no need to get all arsey about it! Anyway I'll wait for someone more pleasant to respond.

 

Firstly Daniels post isn't "arsey" his telling you how it is, and all that statement has done it isolate you (nice job),

Secondly breaking the rules, will never help

17. Users should not "bump" topics that are still on the first page of the forums. If you bump' date=' you must provide additional information.[/quote']

 

I would suggest you should find out what the difference between an associative and a enumerated array is so you have a better idea of what Daniel is explaining, as for someone more pleasant.. I'll see if CV can help

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.