Multidimensional Array Search in PHP

Standard

Working with Arrays

Array’s are a great way to logical group and order large sets of data. Recently, I was working on a project k

To support the search functionality, I figured there would be no easy way to just use array_filter and array_search. Resource cost was also a big factor; for‘s and foreach‘s just wouldn’t work.

Below you will find a multidimensional_array_search function which will return all the arrays that contain the search string within it.

The Function:

The goal of the function is to return the parent array for what the content was found in:

The above function is a smorgasburg of snippets I found on the PHP.Net docs and stackoverflow. I optimized it to return the array that is matched and the Match string to make it as useful as possible. Give it a go, and let me know how it works.

Example:

To test this code, you can use the following sample code:

Summary

This is not the best solution for this, but it works. It’s simple user-land code and gets the job done. If you have a better solution please share!