Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/66299-solved-dynamic-associative-arrays/
Share on other sites

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.

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.

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 :)

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.