0

I'm being handed some data which is not well defined and subject to change (or at least there's no formal spec and properties may be added/removed). I have no control over the data which I receive as JSON and then json_decode into an anonymous object.

In an attempt to make sense of the data, I'd like to build some mappings. Assuming the data defines a person, something like:

$Data = json_decode($TheRawData);

$Mappings->Personal['Firstname']="FirstName";
$Mappings->Personal['Employer']="Employment->CurrentEmployer";

Which, if I were doing it by hand would mean the firstname can be found at $Data->FirstName and the Current Employer is at $Data->Employer->CurrentEmployer

I'd like to store the info for the mappings in the database for maintainability. Unfortunately, when I attempt to parse the object for the defined properties as shown below:

foreach($Mappings->Personal as $Key=>$Value) {
    print $Key . ": " . $Data->{$Value};    
}

The firstname works perfectly (as expected) but it doesn't like the selector for the 2nd entry, presumably because it's spanning multiple objects and would require repeated lookups.

Can someone tell me if there's any way I can map to arbitrary locations in my object?

4
  • Where does $Profile come from? Have I missed something? Commented Mar 28, 2012 at 10:33
  • Thanks Pete. I've changed it now. - Basic Commented Mar 28, 2012 at 10:40
  • 1
    @Pete - Apologies, I hadn't refreshed this page so didn't see your comment - I spotted the mistake and fixed it. Well spotted, twice :) Commented Mar 28, 2012 at 11:24
  • No worries. Just pulling your leg :) Commented Mar 28, 2012 at 14:56

1 Answer 1

1

A quick Google turned up nothing useful, so I'd have to suggest something a little more iterative.

$Data = json_decode($TheRawData);

$Mappings->Personal['Firstname']=array("FirstName");
$Mappings->Personal['Employer']=array("Employment","CurrentEmployer");

foreach($Mappings->Personal as $Key=>$Value) {
    $Result = $Data;
    foreach($Value as $PropertyName) {
        $Result = $Result->$PropertyName;
    }
    print $Key . ": " . $Result;    
}

I've not tested this, but something along these lines should work for you.

Sign up to request clarification or add additional context in comments.

3 Comments

This works, assuming there's only 1 level of nesting - I'd ideally like to be able to support multiple. I could use a recursive function to step through each level but it seems very messy.
Not so. Look carefully at the inner loop. It takes $Result, reads the requested property, and assigns it to $Result, providing the recursion you desire.
Wow, missed comments and mis-read code all in the same morning - Not off to a great start. I think that would work fine, 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.