0

I have two stdClass Object's that have a number of fields. I cannot change the object layout or structure.

For example, they both have a phone number:

Object A

[phone] => Array
(
[0] => stdClass Object
(
[value] => '+1 123 456 7890'
[primary] => 1
)
)

Object B

[phone] => '+1 123 456 7890'

I want to dynamically access them with a database entry. In other words, to access 'phone' in Object B, it is simply:

$objB->phone

And in the database, I would store the field as 'phone', assign this value to a variable e.g. $field and the code would be:

$objB->{"$field"}

The issue arises however if I want to map Object A. To access the phone value, I would need to:

$objA->phone[0]->value

But as I have many fields and many mappings, how do I dynamically set up access to these fields?

Assuming I had a database entry like:

phone.0.value

This could be easily translated to:

$objA->{"$field1"}[$key]->$field2

But what happens in a dynamic or nested case, for example, another field is:

email.company.0.value.0

I'm not aware of a way to access this value dynamically. In other words, how can I build access on the fly for a given database entry?

$objA->{"$field1"}->{"$field2"}[$key1]->$field3[$key2]
5
  • 1
    I asked this quesiton once on stack overflow. The answer was next to nothing. Basically you can have your app either object driven or table driven but not both (at least not cleanly). If you do not sort/filter/join you can use a object style database. If you want to use sort/filter/join you should use associative array instead of object. That's the best answer I could get. Commented Apr 9, 2020 at 15:08
  • Interesting. Do you have a link to your question by any chance? Thanks! Commented Apr 9, 2020 at 15:30
  • I looked after posting that comment but could not find it. I'll take another stab. It may have been deleted as duplicate or something. Commented Apr 9, 2020 at 15:32
  • Still can't find the question but found this interesting bit: stackoverflow.com/questions/17421306/… Commented Apr 9, 2020 at 15:37
  • I edited my Answer. please check it. Commented Apr 9, 2020 at 16:19

1 Answer 1

1

You can create a function for doing this:

function test($str, $c) {
    $res = $c;
    $array = explode('.', $str);
    foreach ($array as $item) {
        if (is_numeric($item)) {
            $res = $res[$item];
        } else {
            $res = $res->$item;
        }
    }
    return $res;
}
// $c is your \StdClass
echo test('email.company.0.value.0', $c);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this doesn't answer the question. Your code assumes that I know in advance how deep/nested I need to go and ignores the fact that I may have arrays. The issue isn't access, I know how to do that, it's how I access dynamically.
Thank, I reviewed this and it appears to work as I would like it to. Very much appreciate the edit! :)

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.