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

Thread Solved

Join Date: Dec 2007
Posts: 68
Reputation: JRSofty is an unknown quantity at this point 
Solved Threads: 10
JRSofty's Avatar
JRSofty JRSofty is offline Offline
Junior Poster in Training

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

 
0
  #1
Mar 14th, 2008
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:

  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:

  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:
  1. level1.level2a.level3=10::
  2. level1.level2b=20
The output array would look like this:
  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?
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

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

 
0
  #2
Mar 14th, 2008
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?
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: JRSofty is an unknown quantity at this point 
Solved Threads: 10
JRSofty's Avatar
JRSofty JRSofty is offline Offline
Junior Poster in Training

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

 
0
  #3
Mar 14th, 2008
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
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

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

 
1
  #4
Mar 14th, 2008
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!)
  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);
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: JRSofty is an unknown quantity at this point 
Solved Threads: 10
JRSofty's Avatar
JRSofty JRSofty is offline Offline
Junior Poster in Training

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

 
0
  #5
Mar 14th, 2008
Hmm yes that is much more efficient than the way I'm doing it.

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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC