I am working with multidimensional, associative arrays a lot, mainly for configuration data and introduced "breadcrumbs" into the use of multidimensional arrays. The basic idea: my classes are designed to handle one-dimensional (flat) breadcrumb-arrays as a reference-guide for certain values inside a multidimensional, associative array. Every incremented index of a breadcrumb is basically a level deeper inside the array until the last level and associative key are found and reached in recursion. E.g.:
$myArray = array(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myBreadcrumb = array('firstLevel', 'secondLevel', 'myValue');
$fancyClass = new \someNamespace\fancyArrayProcessingClass($myArray);
$myValue = $fancyClass->getValueForBreadcrumb($myBreadcrumb);
If it's requested i'll post an example for the processing of the breadcrumbs, too, but since i'm targeting on custom data types i found it being off-topic. It is getting tiresome and is overhead to always code "Wrapper" classes, implemented classes or another kind of construct to make arrays navigable via breadcrumb. I wondered if there is a way to introduce real new DataTypes into PHP that can be handled like actual DataTypes. My idea of a good syntax for this concept:
$myArray = navigableArray(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myBreadcrumb = array('firstLevel', 'secondLevel', 'myValue');
$myValue = $myArray[$myBreadcrumb];
Or even more intuitive to use with just xpath-style strings:
$myArray = navigableArray(
'firstLevel' => array(
'secondLevel' => array(
'myValue' => 42
),
'anotherLevel' => array(
'anotherValue' => 13
)
)
);
$myValue = $myArray['firstLevel/secondLevel/myValue'];
I know that there is a sentence in the PHP documentation that says something like "developers will never need to introduce their own DataTypes into PHP", but AFAIK there is no reason given why it is like that and why a developer is - unlike with almost every other language - unable to introduce fully custom DataTypes.
Edit: For anyone curious: i found an alternative route, with the standard php class "ArrayAccess" you can make your PHP Object behave like an actual array. The famous "Judy" class incorporates "ArrayAccess" and "Iterator" and fits exactly what i was looking for in this question-thread.