搜索返回多个结果的多维数组

Search multi dimensional array returning multiple results

我正在尝试搜索多维数组,但只搜索了 returns 第一次出现。

我有一个多维数组$planned_housek:

Array
(
    [0] => Array
        (
            [id] => 14
            [title] => Titel 10
            [ruletext] => Regel 10
            [room] => 101
            [roomid] => 43
        )

    [1] => Array
        (
            [id] => 14
            [title] => Titel 10
            [ruletext] => Regel 10
            [room] => 102
            [roomid] => 42
        )

    [2] => Array
        (
            [id] => 14
            [title] => Titel 10
            [ruletext] => Regel 10
            [room] => 103
            [roomid] => 41
        )

    [3] => Array
        (
            [id] => 14
            [title] => Titel 10
            [ruletext] => Regel 10
            [room] => 104
            [roomid] => 44
        )

    [4] => Array
        (
            [id] => 14
            [title] => Titel 10
            [ruletext] => Regel 10
            [room] => 105
            [roomid] => 45
        )

    [5] => Array
        (
            [id] => 7
            [title] => TItel 3
            [ruletext] => Regel 3
            [room] => 101
            [roomid] => 43
        )

    [6] => Array
        (
            [id] => 13
            [title] => Titel 9
            [ruletext] => Regel 9
            [room] => 101
            [roomid] => 43
        )
)

当我搜索它时:

$planned_tasks = array_search($bkng_room, array_column($planned_housek, 'roomid'));

它returns第一次出现$bkng_room

我是否必须使用 for/foreach 来获取一组匹配项?还是有更优雅的解决方案(PHP 5.5)?

我发现了这个问题,但答案似乎不是最优雅的解决方案: How to search a multidimensional array to return multiple keys。 这个也没有:Search multidimensional array for value and return new array

array_search 上的文档中,我发现:

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

但我不确定如何在我的代码中实现它。

希望,这个功能就是你想要的!!

function array_search_inner ($array, $attr, $val, $strict = FALSE) {
  if (!is_array($array))
      return FALSE;
   foreach ($array as $key => $inner) {
      if (!is_array($inner))
           return FALSE;
      if (!isset($inner[$attr])) 
           continue;
      if ($strict) {
      if ($inner[$attr] === $val) 
           return $key;
    } else {
      if ($inner[$attr] == $val) return $key;
    }
  }
  return NULL;
}

// Example usage
$key = array_search_inner($array, 'id', 6);
print_r($key);