Search code examples
phpmultidimensional-arrayreferenceassociative-arraycustom-data-type

PHP custom DataType for multidimensional arrays


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.

http://php.net/manual/de/class.arrayaccess.php

http://php.net/manual/de/class.judy.php


Solution

  • Based on your use-case, I'd recommend you look into PHP's SPL Iterators. Specifically the Recursive Iterator interface or the RecursiveArrayIterator implementation. These Iterators are built natively into PHP, are very fast, and many allow you to access them using native PHP functions such as foreach(), for(), count(), etc.

    You can then extend one of the SPL Iterator interfaces to create your own custom class that gives you functionality specific to your Breadcrumb needs. FWIW, I suppose it's true that developers should not have to create their own native data types in PHP (because you can't) but creating custom types with PHP classes is highly encouraged over using native arrays.