943,699 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4806
  • PHP RSS
Mar 14th, 2008
0

Constructing a multi-dimensional array using a dilimited string input.

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. access.read=1,2,3::
  2. access.write=1,2::
  3. access.modify=1,2::
  4. access.manage=1::
  5. 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)
  1. function readConfigString($cnf){
  2. // is the input valid
  3. $out = array();
  4. if(!is_null($cnf) && is_string($cnf)){
  5. // clean up the input
  6. $cnf = trim($cnf);
  7. if(substr($cnf,-2) == '::'){
  8. $cnf = substr($cnf,0,(strlen($cnf) - 2));
  9. }
  10. // breakdown the lines
  11. $lines = split('::',$cnf);
  12. foreach($lines as $line=>$data){
  13. $valSets = split('=',$data);
  14. if(strpos($valSets[0],".") > 1){
  15. $names = split("\.",$valSets[0]);
  16. $fname = trim(strtolower($names[0]));
  17. foreach($names as $id=>$name){
  18. $name = trim(strtolower($name));
  19. if($name != $fname){
  20. if(strpos($valSets[1],",") > 0){
  21. $out[$fname][$name] = split(",",$valSets[1]);
  22. }else{
  23. $out[$fname][$name] = $valSets[1];
  24. }
  25. }
  26. }
  27. }else{
  28. $name = trim($valSets[0]);
  29. if(strpos($valSets[1],",") > 0){
  30. $out[$name] = split(",",$valSets[1]);
  31. }else{
  32. $out[$name] = trim($valSets[1]);
  33. }
  34. }
  35. }
  36. }
  37. return $out;
  38. }

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)
  1. level1.level2a.level3=10::
  2. level1.level2b=20
The output array would look like this:
PHP Syntax (Toggle Plain Text)
  1. Array(
  2. [level1] => Array(
  3. [level2a] => 10
  4. [level3] => 10
  5. [level2b] =>20
  6. )
  7. )

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?
Reputation Points: 16
Solved Threads: 10
Junior Poster in Training
JRSofty is offline Offline
68 posts
since Dec 2007
Mar 14th, 2008
0

Re: Constructing a multi-dimensional array using a dilimited string input.

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?
php Syntax (Toggle Plain Text)
  1. $config = array(
  2. "access" => array(
  3. 'read' => array(1,2,3),
  4. 'write' => array(1,2),
  5. 'modify' => array(1,2)
  6. 'manage' => 1
  7. ),
  8. "somethingelse" => 10101
  9. );
With your method you have all strings. No NULL, boolean or other types.
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008
Mar 14th, 2008
0

Re: Constructing a multi-dimensional array using a dilimited string input.

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
PHP Syntax (Toggle Plain Text)
  1. $config = array(
  2. "access" => array(
  3. 'read' => array(1,2,3),
  4. 'write' => array(1,2),
  5. 'modify' => array(1,2)
  6. 'manage' => 1
  7. ),
  8. "somethingelse" => 10101
  9. );
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.
Reputation Points: 16
Solved Threads: 10
Junior Poster in Training
JRSofty is offline Offline
68 posts
since Dec 2007
Mar 14th, 2008
1

Re: Constructing a multi-dimensional array using a dilimited string input.

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!)
php Syntax (Toggle Plain Text)
  1. $string = "level1.level2a.level3=10";
  2.  
  3. list($combinedKey, $value) = split('=', $string);
  4. $keys = split('\.', $combinedKey);
  5.  
  6. $command = '$config["'.join('"]["', $keys).'"] = $value;';
  7. eval($command);
  8.  
  9. var_dump($config);
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008
Mar 14th, 2008
0

Re: Constructing a multi-dimensional array using a dilimited string input.

Hmm yes that is much more efficient than the way I'm doing it.

I'll give it a shot thanks for the help.
Reputation Points: 16
Solved Threads: 10
Junior Poster in Training
JRSofty is offline Offline
68 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Unable to jump to row 0
Next Thread in PHP Forum Timeline: php email help please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC