![]() |
Array mess
Arrays are one thing I am trying to strengthen my skills on and have come upon a task that has stumped me for a bit now.
I have a database field called parameters, which contains data that looks like... this=that,what=where,how=when I have built a function to seperate the entire string as an array, getting the keys of this, what, and how. It also grabs the keys, that, where, and when. I have a class to do all this, and class vars to contain the keys, to contain the original array, and to contain the values. What I am trying to do and what is stumping me is how to grab the value of a specific key. The constructor process and parses the string into an array by calling the parse($string) method. Then I have another method called getValue($key), which is supposed to get a value of the array by the key. This is all kinda hard to explain, but let me just show you some sloppy code ;) Code:
/**@var array of parameters */ |
Edit, I've modified the code a bit since posting, but for now I've tried array_flip to search the array based on the key ( which after flipped should be the value ) to return the new key ( old val ), but still no luck. I've var_dumped the array and the flipped array to see that the key was there, and sure enough it is and things flipped properly.
- Edit Though I edited my first post, sorry about that. Anyways, I figured out a way. I had a few issues when splitting the original string with whitespace which was throwing off checking for a match. Here's the final code ( not cleaned up )... Code:
class parameters { |
You could do this: $this->_params[$key] in getValue.
Like so: PHP Code:
|
If you're willing and/or able to use some of the SPL features then you code could be whittled down to something like the following (just an example off the top of my head).
PHP Code:
|
Thanks. I've never worked with SPL before, gotta do some reading :D I really should have worked more with arrays other than doing the basics, but this was a good learning experience.
|
| All times are GMT. The time now is 01:59 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0