我有一个问题,即确定包含另一个数组的索引的PHP关联数组是否是另一个类似构造的关联数组的子集,即包含另一个数组的索引的数组。
假设我有两个数组,一个名为 dogs ,另一个名为 legged-animals 。
<?php
$dogs = array(0 => array('height' => 100, 'weight' => 100),
1 => array('height' => 50, 'weight' => 50));
$legged-animals = array(0 => array('height' => 200, 'weight' => 500),
1 => array('height' => 220, 'weight' => 500),
2 => array('height' => 100, 'weight' => 100),
3 => array('height' => 50, 'weight' => 50));
?>
所以问题是,我怎样才能确定狗是 legged-animals 的子集?
编辑: 这是我试图找出一个是否是另一个的子集:
function filter($largeSets, $subSets)
{
$result = array();
$count = count($subSets);
foreach ($largeSets as $individualSet)
{
foreach ($subSets as $set)
{
$intersection = array_intersect($individualSet, $set);
if (!empty($intersection) && isset($intersection['height']) && isset($intersection['weight']))
{
$result['array'][] = $individualSet;
$count--;
break;
}
}
}
$result['result'] = ($count == 0);
return $result;
}
更新:
这是一个更简单的解决方案,我认为这将解决问题。我们的想法是遍历多维数组,serialize
数组,然后使用array_intersect
。
$dogs = array(0 => array('height' => 100, 'weight' => 100),
1 => array('height' => 50, 'weight' => 50),
2 => array('height' => 10, 'weight' => 25) );
$legged_animals = array(0 => array('height' => 200, 'weight' => 500),
1 => array('height' => 220, 'weight' => 500),
2 => array('height' => 100, 'weight' => 100),
3 => array('height' => 50, 'weight' => 50));
foreach ($dogs as $dog)
{
$arr[] = serialize($dog);
}
foreach ($legged_animals as $animal)
{
$arr2[] = serialize($animal);
}
$intersection = array_intersect($arr, $arr2);
print_r($intersection);
此时,intersection
将打印出交集的序列化形式。要获得初始结果,您必须unserialize
数组。
有更简单的方法吗?
答案 0 :(得分:0)
这种冗长的方法怎么样:
// borrowed from giosh at http://php.net/manual/en/function.array-diff-assoc.php
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach($array1 as $key => $value) {
if( is_array($value) ) {
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if( !empty($new_diff) )
$difference[$key] = $new_diff;
}
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
$difference[$key] = $value;
}
}
return $difference;
}
function array_is_sub($needle,$haystack)
{
foreach ($needle as $n)
{
$matched=false;
foreach ($haystack as $h)
{
if (empty(array_diff_assoc_recursive($n, $h)))
{
$matched=true;
break;
}
}
if (!$matched) return false;
}
return true;
}
$dogs = array(0 => array('height' => 100, 'weight' => 100),
1 => array('height' => 50, 'weight' => 50));
$legged_animals = array(0 => array('height' => 200, 'weight' => 500),
1 => array('height' => 220, 'weight' => 500),
2 => array('height' => 100, 'weight' => 100),
3 => array('height' => 50, 'weight' => 50));
if (array_is_sub($dogs,$legged_animals)) {
echo "Dogs is a subset of Legged_Animals.";
} else {
echo "Dogs is NOT a subset of Legged_Animals.";
}
应该工作。
答案 1 :(得分:0)
此解决方案可以完美地满足我的需求:
// takes two multidimensional arrays, $arr1 and $arr2
// and returns the intersection of the two
// @return an array that is the intersection of the two
function findIntersection($arr1, $arr2)
{
$retArray = array();
$firstArray = array();
$secondArray = array();
$intersection = array();
foreach ($arr1 as $set)
{
$firstArray[] = serialize($set);
}
foreach ($arr2 as $set)
{
$secondArray[] = serialize($set);
}
$intersection = array_intersect($firstArray, $secondArray);
if (!empty($intersection))
{
foreach ($intersection as $serializedArray)
{
$retArray[] = unserialize($serializedArray);
}
}
return $retArray;
}
答案 2 :(得分:-1)
array_intersect()可以解决问题(不要在数组名称中使用短划线):
$dogs = array(0 => array('height' => 100, 'weight' => 100),
1 => array('height' => 50, 'weight' => 50));
$leggedanimals = array(0 => array('height' => 200, 'weight' => 500),
1 => array('height' => 220, 'weight' => 500),
2 => array('height' => 100, 'weight' => 100),
3 => array('height' => 50, 'weight' => 50));
print_r( array_intersect( $dogs, $leggedanimals ) );
// Array ( [0] => Array ( [height] => 100 [weight] => 100 ) [1] => Array ( [height] => 50 [weight] => 50 ) )