php-dot-notation icon indicating copy to clipboard operation
php-dot-notation copied to clipboard

Add ability to parse arrays containing objects

Open danielsetreus opened this issue 4 years ago • 1 comments

Considering the following array:

$arr = array(
        'foo' => array(
            'bar' => 'inner value'
        ),
        'fooObj' => (object) array(
            'objAttribute' => 'some value'
        ),
);

$dot = new \Adbar\Dot($arr);

var_dump($dot->has('foo.bar')); // bool(true)
var_dump($dot->has('fooObj.objAttribute')); // bool(false)

Would it be possible to extend Dot to also include objects (nested or not nested inside arrays).

danielsetreus avatar Oct 21 '19 15:10 danielsetreus

Yes it is possible. The only issue that I can think of is how do you know which to use when you flatten and unflatten it?

A user of this library may have to make a choice to use associative arrays or objects, and silently coerce the data structure to be stored consistently and behave consistently.

It's unfortunate that associative arrays and objects are so similar, if you write something to make a class compatible with object and array syntax you have to implement two interfaces to make it friendly for both access methods. 🤒 🤕

Oh yeah, and a function to differentiate an indexed array from an associative array. Do the indexes need to be contiguous and start from 0? Or is the fact they are all numeric good enough? Down the 🐰 hole....

acrois avatar Dec 26 '19 19:12 acrois

Closing due to inactivity.

adbario avatar Sep 25 '22 21:09 adbario