XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 Hi! My current project consists of me creating a database based soley from arrays and kept in memory on a socket server. Sort of like a temporary database without too much overhead (it's for a game I'm working on) Basically, I have created functions called: db_create, db_select, db_delete, and db_insert The one I really need help with is the db_create function. It goes something like this: $db = array(); $db = db_create("table","full/table/path",$db); Now, the db_create function should take the base database ($db) and return the database with the new the associative array appended like ??? ??? $db["full"]["table"]["path"] and each ["path"] should be declared as an array (if that key doesn't exist already); I wrote the function which exploded the path string by "/" and did a foreach loop and tried to combine them but no luck. I tried getting real creative, but I'm spent =}~ I had a function that I thought worked, but it instead created: $db["full"], $db["table"] and $db["path"] which is not what I'm trying to do. I guess I'm having trouble thinking outside the box here. I've tried everything that I think I know how to do. All I need is for this function to be done, then I could use that function to redo all of the other functions, since I could just modify it to insert, delete and search arrays. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/ Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 Wow, surely this can't be that tough for you lot of PHPFreaks Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-331678 Share on other sites More sharing options...
448191 Posted August 23, 2007 Share Posted August 23, 2007 It's just a simple matter of checking if the index exists, and if not create it. Maybe add a little recursion to support deep levels. But why would you want to use a socket server? I doubt much performance is gained compared to simply writing and reading serialized arrays to disk, there is network overhead after all. Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-331718 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 Much performance is gained. I specialize in socket connections and I'm using Flash as the client for the game. It's as simple as keeping the bytes sent both ways as small as possible. I've tried recursing and I'm just missing something somehow. Perhaps you could provide me a small sample, thanks for your reply. Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-331729 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-332225 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 ??? Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-332249 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 Wow, I can't believe I stumped everyone lol. Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-332282 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Author Share Posted August 23, 2007 Well, after about 15 or so hours of messing with this I finally got it. Here is the code so if someone else has the problem: Dynamic Multidimension Arrays: $test_str = "test1/test2/test3"; $exp = explode("/",$test_str); foreach ( $exp as $val ) { if (!isset($sm_array) ) { $sm_array[$val] = array(); $last_array = &$sm_array[$val]; } else { $last_array[$val] = array(); $last_array = &$last_array[$val]; } } //Outputs $sm_array["test1"]["test2"]["test3"] I just put it to a function and called it and finally some magic Quote Link to comment https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/#findComment-332338 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.