搜索数组并在找到匹配项时返回所有键和值

Tim*_*Tim 12 php arrays

我喜欢在数组上执行搜索,并在找到匹配项时返回所有值.[name]数组中的关键是我正在搜索的内容.

Array (
[0] => Array
    (
        [id] => 20120100
        [link] => www.janedoe.com
        [name] => Jane Doe
    )
[1] => Array
    (
        [id] => 20120101
        [link] => www.johndoe.com
        [name] => John Doe
    )
)
Run Code Online (Sandbox Code Playgroud)

如果我搜索了John Doe,它会返回.

Array
(
    [id] => 20120101
    [link] => www.johndoe.com
    [name] => John Doe
)
Run Code Online (Sandbox Code Playgroud)

根据我搜索的内容重命名数组会更容易吗?我也可以生成以下内容而不是上面的数组.

Array (
[Jane Doe] => Array
    (
        [id] => 20120100
        [link] => www.janedoe.com
        [name] => Jane Doe
    )
[John Doe] => Array
    (
        [id] => 20120101
        [link] => www.johndoe.com
        [name] => John Doe
    )
)
Run Code Online (Sandbox Code Playgroud)

iBi*_*kov 15

$filteredArray = 
array_filter($array, function($element) use($searchFor){
  return isset($element['name']) && $element['name'] == $searchFor;
});
Run Code Online (Sandbox Code Playgroud)

需要PHP 5.3.x.