So I'm pulling xml from a bunch of different files. So far I've just been direct outputting it. But there are so many nice functions with arrays, I've thought it would be better to stick the data in an array, and then play with it as needed.
What would be the best type of array? Basically there will 5 pieces of info I'm pulling from each parent node 1)title 2)text and so on.
Before when I've done something similar, I've put every thing in one numeric array, and every 5 items is one unit. So item[0] would be the first one's title, item[4] the second. That works fine, but it feels like a half ass way to do it.
So I could do 5 numeric arrays title[0] text [0]. But that still doesn't feel right.
I'm looking for something like how I access the xml, which would be: $z->item($i)->getElementsByTagName($pub)->item(0)->nodeValue
So would a good way to do a php array something like
$item[0]=>title $item[0]=>text
Some of the things I want to do with them is sort by pub date.
Sorry for the long post!
EDIT: I'm just using $arrayItem[$i]["title"]
Unless someone can tell me a better way.
Yeah PHP has two types of array, numeric and associative (i.e. using non=numeric indices, like a dictionary or hash table) -- it doesn't really distinguish between the two. It's very common practice to use a combination of the two to store structured data -- a numeric array containing 'nested' associative arrays, just as you have.
$foo = array ( array ( 'title' => 'Foo', 'description' => 'The first element' ), array ( 'title' => 'Bar', 'description' => 'The second element' ), array ( 'title' => 'Baz', 'description' => 'The third element' ) )
A 'better' way? Well if you're feeling virtuous you could use objects instead of associative arrays but frankly there's little practical advantage to doing so unless you're working in an OO environment.
here's the list of PHP array functions. Of those, array_multisort or the user sorts (usort etc) will probably be of most interest to you.
edit - I don't know how you're parsing your xml but you might want to look at the xml parser and xml_parse_into_struct
Thanks peeps.
Sticks, sometimes I get the feeling I'm going things half-assed, but I guess I'm whole assed this time.
Tech, I'm using DOM, although I have been tempted to look into xpath. That xml parser looks promising. But I think I'll try to get this to work before trying another road. One of the problems is that not all the xml docs I am pulling from are formatted the same.
To sum up what I'm trying to do. Take multiple xml docs from different sources- grab just the data I need and put into an array. It would be much easier for me to do it with mySQL db, but I'm trying to replicate that through xml/php array.