As part of some ongoing improvements to my Validation Class I had need to be able to validate posted data from dynamically generated forms.
It's still a little rough around the edges and doesn't fully replicate the Xpath syntax, but it gives just enough functionality to be able to easily search a large multidimensional array.
File Name: KP-Array-Xpath-vCurrent.zip
Version: 1.2 (05/12/2012)
File Size: 13kb
Contains: KP-Array-Xpath class and example.
How it works
Lets say we have a form for adding car manufacturers that allows us to add multiple manufacturers and multiple models in one go, which posts the following data...
$testData = array( 'manufacturer' => array( '1' => array( 'name' => 'Ford', 'models' => array( '1' => array( 'model-name' => 'Fiesta', 'model-type' => 'Car', ), '2' => array( 'model-name' => 'Transit', 'model-type' => 'Van', ), ), ), '2' => array( 'name' => 'Volkswagen', 'models' => array( '1' => array( 'model-name' => 'Golf', 'model-type' => 'Car', ), ), ), ), );
Now, lets say we need to validate the model-types to make sure they don't contain any silly characters etc.
We instantiate a new arrXpath object and pass it our data, it will then process that array and produce an array of all the key paths found in the data.
// instantiate a new class and pass it our data $fact = new arrayXpathFactory(); $inst = $fact->createInstance($testData); // this will build the array key paths for the whole data structure Array (  => manufacturer  => manufacturer/1  => manufacturer/1/name  => manufacturer/1/models  => manufacturer/1/models/1  => manufacturer/1/models/1/model-name  => manufacturer/1/models/1/model-type  => manufacturer/1/models/2  => manufacturer/1/models/2/model-name  => manufacturer/1/models/2/model-type  => manufacturer/2  => manufacturer/2/name  => manufacturer/2/models  => manufacturer/2/models/1  => manufacturer/2/models/1/model-name  => manufacturer/2/models/1/model-type )
Now we can filter that data using our xpath like syntax and pull out just the data we need.
// now filter using a wildcard $arrFiltered = $inst->filter('manufacturer/*/models/*/model-type'); // gives us... Array ( [manufacturer/1/models/1/model-type] => Car [manufacturer/1/models/2/model-type] => Van [manufacturer/2/models/1/model-type] => Car )
That's pretty much alll there is to it.
There's also a fully working version of the above example in the download.
|v1.1||05/12/2012||Created this readme.txt!
Changed to use a factory class so it works better with dependency injection.
Changed name of class from arrXpath to arrayXpath.
Updated demo accordingly.
The boring bit, GPL v3 basically, click the logo for more info.