Omzy Posted August 16, 2009 Share Posted August 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/ Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 Come on, where are all you php freaks today lol. Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899490 Share on other sites More sharing options...
Daniel0 Posted August 16, 2009 Share Posted August 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899494 Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 Surely there must be a way of knowing what "position" each sub-array is, for example 'basic' would be position 0 but I can always "plus 1" the position to get what I want... Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899506 Share on other sites More sharing options...
Daniel0 Posted August 16, 2009 Share Posted August 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899509 Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 Crikey, no need to get all arsey about it! Anyway I'll wait for someone more pleasant to respond. Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899515 Share on other sites More sharing options...
Daniel0 Posted August 16, 2009 Share Posted August 16, 2009 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". Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899518 Share on other sites More sharing options...
MadTechie Posted August 16, 2009 Share Posted August 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/170521-multidimensional-array/#findComment-899604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.