Categories : PHP

I think this is very useful function i can imagine 🙂 Search by dynamic key function.

  <?php

        /*
         * Example array
         * [
         *  ["id" => 123, "title" => "test"],
         *  ["id" => 456, "title" => "test2"]
         * ]
         */

  function search($array, $key, $value)
    {
        foreach ($array as $collection) {
            if ($collection[$key] == $value) {
                return $collection;
            }
        }
        return false;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *