sunshineinabag Posted January 22, 2014 Share Posted January 22, 2014 (edited) As my first post, I'm stuck on 2 problems that I've slowly been managing to figure out. I think (or hope) I'm pretty close to the solution but cant seem to get past this hurdle. Fortunately, both use the same XML file. For the first problem, I created a function that reads from a XML file, stores certain data into an array then returns that array. So far, it's storing the proper information, however, when I do var_dump or print_r, the result looks a little wonky, as though there's a nested array when I'm not creating a nested array . function getFileAttrs() { $doc = new DOMDocument('1.0'); $doc->load('newXML.xml'); $filesAttrs = array(); $files = $doc->getElementsByTagName("file"); foreach($files as $file) { $fileAttr = $file->getAttribute("filename"); array_push($filesAttrs, $fileAttr); } return $filesAttrs; } $testarr [] = getFileAttrs(); print_r($testarr); The above print_r returns the following: Array ( [0] => Array ( [0] => file name 9 ) ) When I try to loop through the array, I get a notice stating array to string conversion. For the second problem, which is more complex, I'm keeping track of file uploads for my website, including who uploaded it and when, as well as who re-uploaded a modified version and when (I assume that if they re-uploaded it, they've modified it in some way but I don't actually examine for any content changes, I may do that later). If the file is being uploaded for the first time, then I create the a brand new file element, otherwise, I find the appropriate file element and simply append by adding new newUser and newDate children. I'm able to get the latter part to work perfectly, however, it seems that it ALWAYS appends to a file element, even when it should be creating one. Basically, I'm using the if-else to find whether the filename exists and it should write to that particular file being examined. This is a bit of a work around since I tried using the parentNode variable to no avail. I am free to use simpleXML, however, I would rather use DOM. function appendToXML($filename, $name, $origUser, $origDate, $newUser, $newDate) { $doc = new DOMDocument('1.0'); $doc->load('newXML.xml'); $doc->formatOutput = true; $doc->preserveWhiteSpace = false; $files = $doc->getElementsByTagName("file"); foreach($files as $file) { $fileAttr = $file->getAttribute("filename"); if($fileAttr == $filename) { $createNewUser = $doc->createElement("newUser"); $createNewUserText = $doc->createTextNode($newUser); $createNewUser->appendChild($createNewUserText); $file->appendChild($createNewUser); $createNewDate = $doc->createElement("newDate"); $createNewDateText = $doc->createTextNode($newDate); $createNewDate->appendChild($createNewDateText); $file->appendChild($createNewDate); $doc->documentElement->appendChild($file); $doc->save('newXML.xml'); break; } else { $createFile = $doc->createElement("file"); $createFile->setAttribute("filename", $filename); $createName = $doc->createElement("name"); $createNameText = $doc->createTextNode($name); $createName->appendChild($createNameText); $file->appendChild($createName); $createOrigUser = $doc->createElement("origUser"); $createOrigUserText = $doc->createTextNode($origUser); $createOrigUser->appendChild($createOrigUserText); $file->appendChild($createOrigUser); $createOrigDate = $doc->createElement("origDate"); $createOrigDateText = $doc->createTextNode($origDate); $createOrigDate->appendChild($createOrigDateText); $file->appendChild($createOrigDate); $createNewUser = $doc->createElement("newUser"); $createNewUserText = $doc->createTextNode($newUser); $createNewUser->appendChild($createNewUserText); $file->appendChild($createNewUser); $createNewDate = $doc->createElement("newDate"); $createNewDateText = $doc->createTextNode($newDate); $createNewDate->appendChild($createNewDateText); $file->appendChild($createNewDate); $doc->documentElement->appendChild($file); $doc->save('newXML.xml'); break; } } } And the XML which is giving me all the grief: <?xml version="1.0"?> <users> <file filename="file name 9"> <name>blah</name> <origUser>bleh</origUser> <origDate>blah</origDate> <newUser>bleh</newUser> <newDate>blah</newDate> <newUser>hello</newUser> <newDate>hillo</newDate> </file> </users> As you can see, I add additional newUser and newDate elements, however, the name, origUser and origDate elements are only created once and never altered afterward. Edited January 22, 2014 by sunshineinabag Quote Link to comment Share on other sites More sharing options...
sunshineinabag Posted January 22, 2014 Author Share Posted January 22, 2014 Apologies for the doube post, however, my second problem was actually a simple matter of not properly thinking out my logic. That works like a charm, however, the first problem still has me stumped. Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted January 22, 2014 Share Posted January 22, 2014 (edited) If you don't mean to assign $filesAttrs array (returned by getFileAttrs()) to another nested array outside your function, then remove the left/right square bracket. The current assignment is adding a new array to $testarr, which then containing the function result. Unless you call your function with different .xml files, I think you just want: $testarr = getFileAttrs(); Edited January 22, 2014 by rwhite35 Quote Link to comment Share on other sites More sharing options...
sunshineinabag Posted January 23, 2014 Author Share Posted January 23, 2014 Coming from a Java background, I thought that I had to declare my variable with [] but I'll have to go back and review some of the basic PHP notation but that quick fix worked brilliantly, thank you rwhite35! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.