2

I'm updating software that will import XML file of bank statement to invoicing software. One of the operations is through iteration looking for values in the structure of xml file (which the file itself is converted to associative array at this point).

What I would like to achieve is to set some sort of map in config file for the rest of the software to use, that would show where to look for specific data in that associative array, like date of the transaction, cash amount payed and other.

So first cfg map array:

    "map"=>array(
        "date"=>"ARRAY['exec-date']",
        "amount"=>"ARRAY['amount']['value']"
    );

And then use that map to get appropriate values based (from XML) using structure provided in those key values using Variable-variables:

$amount = ${$map['amount']};

Is that even possible? Or am I that tired and it's very easy and I'm just blocked?

LukasS
  • 176
  • 7
  • Have you tried any simpler solutions? – Sverri M. Olsen Jan 02 '15 at 06:03
  • What do you have in mind as simpler? – LukasS Jan 02 '15 at 06:25
  • Variable variables are never the only way of doing something, and they are usually a bad idea. So, have you considered doing this in a more straight-forward, simpler way? – Sverri M. Olsen Jan 02 '15 at 06:28
  • The simple way is to hardcode those path into every part of the software that uses this data and then update it when bank changes the structure. So this may be simple, but it will take more time to update, and I will have to remember where I've had put all the code which parses those damn xml files. So no, I don't see any simple way of doing that. – LukasS Jan 02 '15 at 11:43

1 Answers1

2

You can use anonymous functions:

$map = array(
    "date" => function($x) { return $x['exec-date']; },
    "amount" => function($x) { return $x['amount']['value']; }
);

Then you would do:

$amount = $map['amount']($xml);
Barmar
  • 741,623
  • 53
  • 500
  • 612