Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sifting through a large array,

Status
Not open for further replies.

Shukusei

Programmer
Sep 6, 2004
43
0
0
CA
Okay, in my last post, I asked for help with why I wasn't getting anything out of my script, and found it to be because I wasn't sending the right information through to the array.

Well, now I have the array working, and giving me everything I need, only I have two problems; the array is absolutly huge, and it doesn't seem to be in the right order.

What I'm working with is an XML file used for updating a journal. I'm using Active-link IORCA's XML functions to work with the file, and currently have an array of the XML tree for the entire XML document, which is where the first problem comes in; the array holds keys and values for every little piece of the XML tree.

Now, as I said in my last topic, I stated I'm new to PHP. So I went back to the PHP online manuel, and looked through the functions, namely foreach. I realized that I might be able to use foreach to sift through the array and get only the parts of it I needed. Though I've just realised two things with that.

One, as I've said, the array is huge, and might take a while for the foreach to sift through. Two, the array isn't in the same order as the actual XML document. The document is written with the newest entries at the top of the file, but yet the array is outputting the informartion with the second to newest entry first, then going down to the last, and then giving the newest.


So, my question is twofold; how do I get the array to come up in the right order, and is there a faster way than foreach?
 
foreach() is designed for arrays. Its the fastest way of working with them.

What you can do, though, is work with more advanced arrays. You can split them up into sections, depending on what you're trying to do. For example, instead of having:

Code:
$array = ("1", "2", "3", "a", "b", "c");

Have:
Code:
$array = ("numbers" => array("1", "2", "3"), "letters" => array("a", "b", "c"));

To access a value, you would use:
Code:
$value = $array['numbers'][0];

If you split up your arrays into two sections, the code will run through twice as fast :)

PHP offers many functions for sorting arrays. The most common is sort(), but there are many more, depending on how you want your array to look like, and how it is set up. Look at the array sort functions at php.net
 
I don't think that would work...


Here's the print_r of the array:

All I need is the sections with the tagname being date, mood, music, subject, entry, and then their corresponding values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top