PHP search array in array

Sometimes PHP can be such an old fart. Ever wanted to search for $haystack in an $array which is an array in another array without writing more than one line of code?

One liner for a simple $array – classic:

$bool = in_array($anything, $array);

One liner for multidimensional array for PHP >= 5.5.0

$array = array_search($anything, array_column($array, 'column_key'));

One liner for multidimensional array for PHP < 5.5.0: https://github.com/ramsey/array_column

You’ll have to use Composer, good luck! :-)

Advertisements

Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s