在更大的关联数组中匹配部分关联数组
Matching partial assoc array in bigger assoc array
我有两个数组:
$array1 = [
'user_id' => '1234567890',
'customer_id' => [
'889599494514048876' => [admin => true],
'006994743818411627' => [admin => false],
'259960192293339276' => [admin => false]
]
];
$array2 = [
'customer_id' => [
'259960192293339276' => [admin => false]
]
];
我想检查 array1 中是否存在 array2。
“理想情况下”解决方案是动态的,因此无论数组结构如何嵌套或不同,它仍然有效。尽管如此,任何解决方案或想法仍然非常受欢迎,因为我很困惑。
我看过 array_intersect 和 array_uintersect,但我还没有让它们像我想要的那样工作。
你可以只使用一个简单的递归函数。
对于给定的数据:
$array1 = [
'user_id' => '1234567890',
'customer_id' => [
'889599494514048876' => ['admin' => true],
'006994743818411627' => ['admin' => false],
'259960192293339276' => ['admin' => false]
]
];
$array2 = [
'customer_id' => [
'259960192293339276' => ['admin' => false]
]
];
这个函数应该可以,看代码注释:
function isArraySubSetOfOtherArray(array $childArray, array $potentialParent): bool
{
foreach ($childArray as $key => $value) {
// If the other array doesn't have this key, fail
if (!array_key_exists($key, $potentialParent)) {
return false;
}
// Make sure the values are the same type, otherwise fail
if (gettype($value) !== gettype($potentialParent[$key])) {
return false;
}
// For scalar types, test them directly
if (is_scalar($value)) {
if ($value !== $potentialParent[$key]) {
return false;
}
continue;
}
// For array, recurse into this same function
if (is_array($value)) {
if (!isArraySubSetOfOtherArray($value, $potentialParent[$key])) {
return false;
}
continue;
}
// For anything else, fail or write some other logic
throw new RuntimeException('Unsupported type');
}
// The loop passed without return false, so it is a subset
return true;
}
assert(isArraySubSetOfOtherArray($array2, $array1));
我有两个数组:
$array1 = [
'user_id' => '1234567890',
'customer_id' => [
'889599494514048876' => [admin => true],
'006994743818411627' => [admin => false],
'259960192293339276' => [admin => false]
]
];
$array2 = [
'customer_id' => [
'259960192293339276' => [admin => false]
]
];
我想检查 array1 中是否存在 array2。
“理想情况下”解决方案是动态的,因此无论数组结构如何嵌套或不同,它仍然有效。尽管如此,任何解决方案或想法仍然非常受欢迎,因为我很困惑。
我看过 array_intersect 和 array_uintersect,但我还没有让它们像我想要的那样工作。
你可以只使用一个简单的递归函数。
对于给定的数据:
$array1 = [
'user_id' => '1234567890',
'customer_id' => [
'889599494514048876' => ['admin' => true],
'006994743818411627' => ['admin' => false],
'259960192293339276' => ['admin' => false]
]
];
$array2 = [
'customer_id' => [
'259960192293339276' => ['admin' => false]
]
];
这个函数应该可以,看代码注释:
function isArraySubSetOfOtherArray(array $childArray, array $potentialParent): bool
{
foreach ($childArray as $key => $value) {
// If the other array doesn't have this key, fail
if (!array_key_exists($key, $potentialParent)) {
return false;
}
// Make sure the values are the same type, otherwise fail
if (gettype($value) !== gettype($potentialParent[$key])) {
return false;
}
// For scalar types, test them directly
if (is_scalar($value)) {
if ($value !== $potentialParent[$key]) {
return false;
}
continue;
}
// For array, recurse into this same function
if (is_array($value)) {
if (!isArraySubSetOfOtherArray($value, $potentialParent[$key])) {
return false;
}
continue;
}
// For anything else, fail or write some other logic
throw new RuntimeException('Unsupported type');
}
// The loop passed without return false, so it is a subset
return true;
}
assert(isArraySubSetOfOtherArray($array2, $array1));