Phabstractic: Array Utilities

asherwunk/phabstractic has some new array utilities in its resource folder

Array Utilities

The trouble with some of the array methods in PHP is that they don’t always work or cover possibilities that Phabstractic runs into.  For example, in a sorting function you want to be able to compare objects, or maybe you want to change the case of all the values of an array as opposed to the keys using array_change_key_case().  As well, what if you want to compare objects in a custom fashion when you modify an array to only have unique elements?  This could be important when implementing something such as a map.

The other issue is that this code, when included in primus2/falcraft either didn’t work, or doesn’t work with PHP 7.  I have updated the code so that it does work appropriately in different versions and passes unit tests.  Actually, unit testing helped me discover this, so huzzah!

So, I created a utility class with some static methods to do just this.  I have inherited from zend/std-lib to take advantage of the array methods provided by Zend Framework Standard Library.  The composer.json file has been updated as well to include this Zend library.

Compare Two Elements (Including Objects)

I developed a method to compare two values, including objects, in a custom fashion that is more compatible with the rest of the Phabstractic library.  I tried using spl_object_hash()  in order to tell objects apart, and according to my unit tests it seems to work correctly.  Following is the comparison code (view on github):

Return An Array Of Unique Values

Now that we can compare two elements, I’d like to implement a method that can return unique values in an array using this comparison.  I discovered elsewhere in the phabstractic library that array_unique()  has some limitations, including object comparison.  In version 4 I added a default argument (for backwards compatibility, so that old method calls still work) for associative arrays.  One of the things the old version didn’t do was update the indices of the array, which isn’t a big deal, but made it difficult to unit test against.  The new version now re-orders the numerical indices of a non-associative array.  Following is the returnUnique code, along with a version that operates in a reference friendly way (view on github):

Object To Array To Object

I also created some methods to convert arrays to stdClass  objects, and those objects back to arrays.  I was inspired by code from if-not-true-then-false, however, I discovered that that code was not working with my current incarnation of the library.  I suspected if it might be because of PHP 7, but I wasn’t sure.  Unfortunately, I had to re-write it as the values of the objects and arrays weren’t being converted.  Here’s the code (view on github):

Conclusion

It might not be the wisest idea to inherit zend-stdlib into a class named specifically for arrays, but I did so for backwards compatibility. When I first wrote this framework I made some blunders in terms of organization, but now I’m more integrated with phpunit and Travis CI.  In reviewing and reformatting this code for phabstractic I plan to oversee any design blunders I may have made.

These methods are very useful for arrays, and overcomes some limitations that object conversion and array_unique have.  I would recommend using these functions instead of the built-in PHP functions for these purposes.

This is part of the Phabstractic Library.

If you appreciate my programming please help support me through my Patreon.

photo credit: Shore Club 250 via photopin (license)

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: