flat file and PHP array

Posted on 2005-04-18
Medium Priority
Last Modified: 2012-06-27
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")

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

Level5 keywords

Thank you for your help.
Question by:rblampain
  • 2
  • 2
  • 2
  • +1
LVL 14

Expert Comment

ID: 13813027
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:

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.

Expert Comment

ID: 13813147
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.

Author Comment

ID: 13813414
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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Accepted Solution

cracky earned 2000 total points
ID: 13813437
How silly of me. I totally forgot about serialize()


Just write your array to file with the contents serialized and use unserialize() when you want to read the file back.

Make sure you look at the user contributed notes for serialize() to see examples of how to write these to file. especially from corey at eyewantmedia dot com.

Expert Comment

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


Expert Comment

ID: 13815949
Serialize is definetly the way to go. The best simplest solutions are often the best. Go cracky!

Author Comment

ID: 13824276
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question