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?

4
  • Have you tried any simpler solutions? Commented Jan 2, 2015 at 6:03
  • What do you have in mind as simpler? Commented Jan 2, 2015 at 6: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? Commented Jan 2, 2015 at 6: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. Commented Jan 2, 2015 at 11:43

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, using create_function() I was manage to achieve that. Thank you :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.