Link to home
Start Free TrialLog in
Avatar of rblampain
rblampain

asked on

flat file and PHP array

I need to make a script that creates a multi dimensions array using "forms". The user would be asked to define the number of elements and dimensions and provide keywords for each, this can go down 5,  6 or 7 levels (array of arrays of arrays of...). This needs to be saved as a flat file

Then I need a second script that can read the file and reconstruct the array as it was intended.

I can't visualize how to do this,  could anyone suggest  scripts?

Example of the keywords hierarchy I am talking about:
Primary keywords
("Copyright","About the author","Credits","Preface","Acknowledgments","Contents","Appendices","Index","etc")

Secondary keywords
Copyright ("Liability","Waranty","Trademark","etc")
About the author ("Identity"," Address","Qualifications","etc")
Credits ("Acquisition","Development","Technical","Production","etc")
Preface ("comment1","comment2","etc")
...
Contents ("part1","part2","etc")
etc

Level3 keywords
...
Credits ["Acquisition"] ("research1","research2","etc")
Contents ["Part1"] ("tome1","tome2","etc")
....
Level4 keywords
...
Contents ["part1"]["tome1"] ("chapter1","chapter2","etc")
...
etc

Level5 keywords
etc

Thank you for your help.
Avatar of cracky
cracky

This sounds like a job suited to XML. I would walk through each array and possible sub-array, then write the results to an XML file:
http://phpxmlclasses.sourceforge.net

I would then use one of the parse classes to read the XML file back and populate the array to do with as you wish.

If you don't know how deep the arrays go, then you need to perform a check at each level to see if the next node is an array with is_array(). Then you would use something like array_walk() to perform operations on each iteration.
I have to agree witch cracky. XML is the way to go. Use either the xml functions provided by PHP or the sourceforge class that cracky mentioned.
For parsing the array, you can also write a recursive function that calls itself when it finds an array in an array.
Avatar of rblampain

ASKER

Thanks to both. I know nothing about xml, I quick search shows there is a lot I would have to learn and I just wouldn't have the time.
Any suggestion, any example?
ASKER CERTIFIED SOLUTION
Avatar of cracky
cracky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was thinking of an easier way to store it in a file, such as a delimited text file for example rather than XML, however it would no doubt be a pain and probably unsuited to the types of data you wish to store, so i'd definately go with cracky's idea of using serialize() and unserialize() i'd never looked at these function before, but I'm sure glad I read this post, thanks for helping me also cracky :)

Cheers
Serialize is definetly the way to go. The best simplest solutions are often the best. Go cracky!
By popular demand.
I still have to try it, I'm slowly coming to it because other solutions are not as simple as they look.