This post is just some quick functions that can help you in working with arrays. These are especially helpful when working with document oriented databases such as MongoDB where the schema may not always be consistent between data.

Array Element

When working with nested arrays it helps to be able to dive into the data and provide a fallback if the element you want does not exist. It allows you to dive as far as you want into an array by using a dot notation.

For example if you wanted to access address information inside of an array you might do the following.

Array Sort

This function allows you to sort an array by the value of any of the array's subelements. It works in conjunction with the array_element() function above so you can sort deep within arrays.

Here is the example of how you may use this sort function with nested arrays.

Just so you know I have contributed both of these functions to FuelPHP's array class. If you have not heard of FuelPHP I recommend it highly as it borrows some of the best ideas from CodeIgniter, Kohana, and Rails. Thats really it. Let me know what you think in the comments.

blog comments powered by Disqus