doufang8282 2015-03-06 16:00
浏览 32
已采纳

PHP:检查另一个数组中是否存在嵌套数组值,同时忽略其他值

Given the following haystack and needle:

$haystack = array(
  'foo' => array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
  ),
);

$needle = array(
  'foo' => array(
    1 => 'one',
    2 => 'two',
  ),
);

I want to check if all nested key-value pairs of the needle occur in the haystack (like it does in the example above), while ignoring any additional key-value pairs that may exist in the haystack (like $haystack['foo'][3] in the example).

There are many similar questions on SO but I haven't found a solution for this specific use case. Is there a (combination of) standard PHP functions to do this? What is the most elegant solution?

[update]

I didn't make clear yet that the arrays may not always have the same depth. Also, the keys of the elements in the arrays may be different every time.

  • 写回答

2条回答 默认 最新

  • dqmfo84644 2015-03-06 16:12
    关注

    This is what I came up with myself. It works, but it seems more complex than it should be...

    /**
     * Helper function which recursively checks if the key-value pairs in one array
     * are all present in another array. If all key-value pairs in the needle are
     * present in the haystack, and the haystack also contains additional items,
     * the check wil still pass.
     *
     * @param array $needle
     *   The array with the key-value pairs to look for.
     * @param array $haystack
     *   The array in which to look for the key-value pairs.
     * @return bool
     *   TRUE if all key-value pairs of the needle occur in the haystack. FALSE if
     *   one or more keys or values are missing or different.
     */
    function array_contains(array $needle, array $haystack) {
      // First, check if needle and haystack are identical. In that case it's easy.
      if ($needle === $haystack) {
        return TRUE;
      }
      foreach ($needle as $key => $value) {
        // If the key does not occur in the haystack, we're done.
        if (!isset($haystack[$key])) {
          return FALSE;
        }
        // If the value is an array...
        if (is_array($value)) {
          // ...see if the counterpart in $haystack is an array too...
          if (!is_array($haystack[$key])) {
            return FALSE;
          }
          // ...and if so, recurse.
          if (array_contains($value, $haystack[$key]) == FALSE) {
            return FALSE;
          }
        }
        // If the values are not arrays and not the same, the check fails.
        else if ($value != $haystack[$key]) {
          return FALSE;
        }
      }
      // If we still didn't fail, all tests have passed.
      return TRUE;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看