| | |
Constructing a multi-dimensional array using a dilimited string input.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi everyone,
I'm a lazy programmer and when I want to create an array that I can store serialized in a database I don't feel like typing out all the array stuff rather I want to use a delimited string something like this:
So I came up with this function that takes just that type of text information and converts it into a multi-dimensional array that I can later serialized and store into the database. The code looks like this:
The problem I've got is that it is really limited to only two associated array elements deep. For example if I would input a string like this:
The output array would look like this:
I'm wondering if anyone out there could help me make this a little more flexible in how it handles the associated names so that there could be more than two levels?
I'm a lazy programmer and when I want to create an array that I can store serialized in a database I don't feel like typing out all the array stuff rather I want to use a delimited string something like this:
PHP Syntax (Toggle Plain Text)
access.read=1,2,3:: access.write=1,2:: access.modify=1,2:: access.manage=1:: somethingelse=10101
So I came up with this function that takes just that type of text information and converts it into a multi-dimensional array that I can later serialized and store into the database. The code looks like this:
php Syntax (Toggle Plain Text)
function readConfigString($cnf){ // is the input valid $out = array(); if(!is_null($cnf) && is_string($cnf)){ // clean up the input $cnf = trim($cnf); if(substr($cnf,-2) == '::'){ $cnf = substr($cnf,0,(strlen($cnf) - 2)); } // breakdown the lines $lines = split('::',$cnf); foreach($lines as $line=>$data){ $valSets = split('=',$data); if(strpos($valSets[0],".") > 1){ $names = split("\.",$valSets[0]); $fname = trim(strtolower($names[0])); foreach($names as $id=>$name){ $name = trim(strtolower($name)); if($name != $fname){ if(strpos($valSets[1],",") > 0){ $out[$fname][$name] = split(",",$valSets[1]); }else{ $out[$fname][$name] = $valSets[1]; } } } }else{ $name = trim($valSets[0]); if(strpos($valSets[1],",") > 0){ $out[$name] = split(",",$valSets[1]); }else{ $out[$name] = trim($valSets[1]); } } } } return $out; }
The problem I've got is that it is really limited to only two associated array elements deep. For example if I would input a string like this:
PHP Syntax (Toggle Plain Text)
level1.level2a.level3=10:: level1.level2b=20
PHP Syntax (Toggle Plain Text)
Array( [level1] => Array( [level2a] => 10 [level3] => 10 [level2b] =>20 ) )
I'm wondering if anyone out there could help me make this a little more flexible in how it handles the associated names so that there could be more than two levels?
JRSofty Programming | .NET Dreaming | GalahTech
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
Hi JRSofty,
before I think about a solution to what you want, let me cast some doubts on it first:
Is it really so time saving to have your config code instead of good ol' php?
With your method you have all strings. No NULL, boolean or other types.
before I think about a solution to what you want, let me cast some doubts on it first:
Is it really so time saving to have your config code instead of good ol' php?
php Syntax (Toggle Plain Text)
$config = array( "access" => array( 'read' => array(1,2,3), 'write' => array(1,2), 'modify' => array(1,2) 'manage' => 1 ), "somethingelse" => 10101 );
Petr 'PePa' Pavel
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Ah well you see the config array needs to be read from the database. This is because that each module will have its own configuration. I could very well write things like
quickly but if I want to store it in the database I would need to serialize it. Right now I'm writing the configuration directly into the database because I'm still developing and haven't a way to enter configuration from the application as yet. This code is basically to assist me when I'm setting up the starting point of configuration array. So in the overall yes it does save me a bit of time especially when I want to make changes. As it is right now if I want to store the array in the database without my code above I would have to first write the array out like you have done, then I have to serialize it and output it. I then copy and paste it to the correct field of the database. This is where the time gets added.
I will later design and build an installer for modules where if I write the configuration as in the plaintext I can place it between a couple of XML tags. During the install process it takes the text and makes it into an array and then serializes the array, which of course is the preferred way of storing an array to the database.
At this point two text elements deep is working ok. I'm just wondering if there is a way of making my code work with more than just two named elements with the expressed syntax of my original example.
PHP Syntax (Toggle Plain Text)
$config = array( "access" => array( 'read' => array(1,2,3), 'write' => array(1,2), 'modify' => array(1,2) 'manage' => 1 ), "somethingelse" => 10101 );
I will later design and build an installer for modules where if I write the configuration as in the plaintext I can place it between a couple of XML tags. During the install process it takes the text and makes it into an array and then serializes the array, which of course is the preferred way of storing an array to the database.
At this point two text elements deep is working ok. I'm just wondering if there is a way of making my code work with more than just two named elements with the expressed syntax of my original example.
JRSofty Programming | .NET Dreaming | GalahTech
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
All right, I can see that you know what you're doing :-)
Since your hacking it anyway, we won't mind using eval(), will we? (yuck!)
Since your hacking it anyway, we won't mind using eval(), will we? (yuck!)
php Syntax (Toggle Plain Text)
$string = "level1.level2a.level3=10"; list($combinedKey, $value) = split('=', $string); $keys = split('\.', $combinedKey); $command = '$config["'.join('"]["', $keys).'"] = $value;'; eval($command); var_dump($config);
Petr 'PePa' Pavel
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Hmm yes that is much more efficient than the way I'm doing it.
I'll give it a shot thanks for the help.
I'll give it a shot thanks for the help.
JRSofty Programming | .NET Dreaming | GalahTech
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
If your question is solved then mark the thread solved. If someone gives you good advice then give them some rep.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Unable to jump to row 0
- Next Thread: php email help please
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time display dynamic echo email error file files folder form forms function functions global google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail memmory menu mlm mod_rewrite msqli_multi_query multiple mycodeisbad mysql navigation oop parameter paypal pdf php problem query radio random recourse recursion regex registrationform remote script search seo server sessions sms soap source space sql static syntax system table tutorial update upload url validation validator variable video web webdesign wordpress xml youtube





