搜索数组并返回数组

How i can return the an array if it matches with the one particular field.

To make it more clear, suppose i have an array which looks like so ---

[1] => Array
    (
        [id] => 11
        [category] => phone cases
        [sale_price] => 90,99
        [price] => 120
        [product_name] => "iphone 6 plus" case transparent
    )

[2] => Array
    (
        [id] => 13
        [category] => shoes
        [sale_price] => 180,99
        [price] => 200
        [product_name] => blue platform shoes
        )

[3] => Array
    (
        [id] => 14
        [category] => wallet
        [sale_price] => 150
        [price] => 250
        [product_name] => valvet wallet

So what i want here is that if i search for valvet it should search in the array's [product_name] field and return the array with other fields as well.

For example ---

if i search for valvet it should return --

[0] => Array
    (
        [id] => 14
        [category] => wallet
        [sale_price] => 150
        [price] => 250
        [product_name] => valvet wallet
    )

This is my PHP code, what i am trying to do --

$data = 'plus'; // what i want to search
$search = my_array_search($all_data, $data);

    function my_array_search($array, $string)
{
    $ret = false;
    $pattern = preg_replace('/\s+/', ' ', preg_quote($string, '/'));
    foreach($array AS $k => $v) {
      $res = preg_grep('/' . $pattern . '/', $v);
      if(!empty($res)) $ret[$k] = $res;
    }

    return $ret;
}

But it is only returning the ["product_name"] not the array that matches !!

How can i fix that can someone help me to fix this issue !!!

Why not fill $ret with $v instead?

function my_array_search($array, $string)
{
    $ret = false;
    $pattern = preg_replace('/\s+/', ' ', preg_quote($string, '/'));
    foreach($array AS $k => $v) {
      $res = preg_grep('/' . $pattern . '/', $v);
      if(!empty($res)) $ret[$k] = $v;
    }

    return $ret;
}

preg_grep — Return array entries that match the pattern

So in your case it returns product_name. And you need the whole array, you searched ie. $v:

if (!empty($res)) $ret[$k] = $v;