Hi, everyday I get a xml file that looks something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account-information report-date="2012-12-31T23:59:59.000+01:00" xmlns="http://www.XXX.SE/XXX">

Today I import each account to our db with only the latest application-array (application-date). The code looks something like this:

  $xml = new SimpleXMLElement('../../../../analys/BaseAccount-19.2.xml', null, true);

    foreach($xml->loanaccount as $mess){
        $accountnumber = $mess->{'account-number'};
        $accountstatus = mysql_real_escape_string($mess->{'account-status'});
                $applicationdate = '';
                    foreach($mess->{'application-array'} as $aa){
                        $totaltlanebelopp +=  mysql_real_escape_string($aa->{'application-amount'}); 
                            if((string) $aa->{'applicationdate'} > $applicationdate){
                                $noterefno =  mysql_real_escape_string($aa->{'note-ref-no'});
                                $applicationtype =  mysql_real_escape_string($aa->{'application-type'});
                                $applicationdate =  mysql_real_escape_string($aa->{'application-date'});
        $firstname= mysql_real_escape_string($mess->{'account-information-type'}->{'first-name-main'});
        $lastname= mysql_real_escape_string($mess->{'account-information-type'}->{'last-name-main'});

        $results= $DBH->prepare("insert ignore INTO XXX(...) VALUES (...)")or die(mysql_error());

What I want to do is to insert every 'application-array' in an own row instead of having the 'account-number' with only the latest 'application-array'. I would create a new unique identifier consisting of 'account-number' and 'application-date'.

So in the above case I would like my db to look like this:


Any ideas on how to change my query to get this result?


Instead of doing a single query at the end, do a query inside the innermost loop where you are iterating through each application-array object. I'd recommend just having the account number and application date as separate fields though. You can then create a composite key on these to uniquely identify a row.

Thanks for your reply! I honestly don't know/understand what that query (in the innermost loop) would look like. I would be eternally grateful if you could give me a hint :)


By hint do you mean exact code. It doesn't get much clearer than that.

You would just execute the query from INSIDE the if statement rather than at the end.

*Edit if you can't do that (because the first name is only given at the end) you could create some temp storage (array) then loop through the array at the end.