我需要一个PHP函数,可以断言两个数组是相同的,同时忽略指定的一组键的值(只有值,键必须匹配)。
实际上,数组必须具有相同的结构,但有些值可以忽略。
例如,考虑以下两个数组:
Array
(
[0] => Array
(
[id] => 0
[title] => Book1 Title
[creationDate] => 2013-01-13 17:01:07
[pageCount] => 0
)
)
Array
(
[0] => Array
(
[id] => 1
[title] => Book1 Title
[creationDate] => 2013-01-13 17:01:07
[pageCount] => 0
)
)
如果我们忽略键id
的值,它们是相同的。
我还要考虑嵌套数组的可能性:
Array
(
[0] => Array
(
[id] => 0
[title] => Book1 Title
[creationDate] => 2013-01-13 17:01:07
[pageCount] => 0
)
[1] => Array
(
[id] => 0
[title] => Book2 Title
[creationDate] => 2013-01-13 18:01:07
[pageCount] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[title] => Book1 Title
[creationDate] => 2013-01-13 17:01:07
[pageCount] => 0
)
[1] => Array
(
[id] => 3
[title] => Book2 Title
[creationDate] => 2013-01-13 18:01:07
[pageCount] => 0
)
)
因为我需要它进行测试,所以我提出了以下类,它扩展了PHPUnit_Framework_TestCase并使用它的assert函数:
class MyTestCase extends PHPUnit_Framework_TestCase
{
public static function assertArraysSame($expected, $actual, array $ignoreKeys = array())
{
self::doAssertArraysSame($expected, $actual, $ignoreKeys, 1);
}
private static function doAssertArraysSame($expected, $actual, array $ignoreKeys = array(), $depth, $maxDepth = 256)
{
self::assertNotEquals($depth, $maxDepth);
$depth++;
foreach ($expected as $key => $exp) {
// check they both have this key
self::assertArrayHasKey($key, $actual);
// check nested arrays
if (is_array($exp))
self::doAssertArraysSame($exp, $actual[$key], $ignoreKeys, $depth);
// check they have the same value unless the key is in the to-ignore list
else if (array_search($key, $ignoreKeys) === false)
self::assertSame($exp, $actual[$key]);
// remove the current elements
unset($expected[$key]);
unset($actual[$key]);
}
// check that the two arrays are both empty now, which means they had the same lenght
self::assertEmpty($expected);
self::assertEmpty($actual);
}
}
doAssertArraysSame
遍历其中一个数组,并递归断言两个数组具有相同的键。它还检查它们是否具有相同的值,除非当前键在要忽略的键列表中。
为了确保两个数组具有完全相同的元素数,在迭代过程中删除每个元素,并在循环结束时检查两个数组是否为空。
用法:
class MyTest extends MyTestCase
{
public function test_Books()
{
$a1 = array('id' => 1, 'title' => 'the title');
$a2 = array('id' => 2, 'title' => 'the title');
self::assertArraysSame($a1, $a2, array('id'));
}
}
我的问题是:有没有更好或更简单的方法来完成这个任务,也许使用一些已经可用的PHP/PHPUnit函数?
编辑:请记住我不一定要PHPUnit的解决方案,如果有一个普通的PHP函数可以做到这一点,我可以在我的测试中使用它
我不确定这是否比您已经使用的解决方案更好,但是当我有这个确切的需求时,我以前使用过类似的类。它能够给您一个简单的真或假响应,并且不耦合到测试框架,这对您来说可能是也可能不是一件好事。
class RecursiveArrayCompare
{
/**
* @var array
*/
protected $ignoredKeys;
/**
*
*/
function __construct()
{
$this->ignoredKeys = array();
}
/**
* @param array $ignoredKeys
* @return RecursiveArrayCompare
*/
public function setIgnoredKeys(array $ignoredKeys)
{
$this->ignoredKeys = $ignoredKeys;
return $this;
}
/**
* @param array $a
* @param array $b
* @return bool
*/
public function compare(array $a, array $b)
{
foreach ($a as $key => $value) {
if (in_array($key, $this->ignoredKeys)) {
continue;
}
if (!array_key_exists($key, $b)) {
return false;
}
if (is_array($value) && !empty($value)) {
if (!is_array($b[$key])) {
return false;
}
if (!$this->compare($value, $b[$key])) {
return false;
}
} else {
if ($value !== $b[$key]) {
return false;
}
}
unset($b[$key]);
}
$diff = array_diff(array_keys($b), $this->ignoredKeys);
return empty($diff);
}
}
和一些基于你提供的数组的例子:
$arr1 = array(
'id' => 0,
'title' => 'Book1 title',
'creationDate' => '2013-01-13 17:01:07',
'pageCount' => 0
);
// only difference is value of ignored key
$arr2 = array(
'id' => 1,
'title' => 'Book1 title',
'creationDate' => '2013-01-13 17:01:07',
'pageCount' => 0
);
// has extra key
$arr3 = array(
'id' => 1,
'title' => 'Book1 title',
'creationDate' => '2013-01-13 17:01:07',
'pageCount' => 0,
'extra_key' => 1
);
// has extra key, which is ignored
$arr4 = array(
'id' => 1,
'title' => 'Book1 title',
'creationDate' => '2013-01-13 17:01:07',
'pageCount' => 0,
'ignored_key' => 1
);
// has different value
$arr5 = array(
'id' => 2,
'title' => 'Book2 title',
'creationDate' => '2013-01-13 17:01:07',
'pageCount' => 0
);
$comparer = new RecursiveArrayCompare();
$comparer->setIgnoredKeys(array('id', 'ignored_key'));
var_dump($comparer->compare($arr1, $arr2)); // true
var_dump($comparer->compare($arr1, $arr3)); // false
var_dump($comparer->compare($arr1, $arr4)); // true
var_dump($comparer->compare($arr1, $arr5)); // false
编辑
使用这样一个单独的类的好处是,可以直接对这个类进行单元测试,以确保它的行为符合预期。如果您不能保证工具正常工作,您就不希望依赖于这些工具进行测试。
可以查询数组元素
foreach ($array1 as $index => $subArray)
{
$this->assertEquals($array1[$index]['title'], $array2[$index]['title');
$this->assertEquals($array1[$index]['creationDate'], $array2[$index]['creationDate');
$this->assertEquals($array1[$index]['pageCount'], $array2[$index]['pageCount');
}