我正在寻找niftier检查multidim。数组$ a匹配multidim中的模式(键/值)。 array $ b(换句话说,检查$ b是$ a的子数组)。我的递归遍历代码附带了跟踪帮助,准备粘贴在空脚本中。
需要使用其所选属性的mdim数组来过滤mdim产品数组。
任何简化都表示赞赏。
<pre>
<?php
$a = array (
"a" => "1",
"b" => "red",
"c" => array(
"ca" => "ca123",
"cb" => "cb123",
),
"d" => array(
"da" => "da123",
"db" => "db123",
"dc" => array(
"dca" => "dca123",
"dcb" => "dcb123"
),
),
);
$b = array(
"b" => "red",
"c" => array(
"ca" => "ca123",
),
"d" => array(
"dc" => array(
"dca" => "dca123",
),
),
);
function is_subarray(&$needle = array(), &$haystack = array())
{
if (!$needle || !$haystack || !is_array($needle) || !is_array($haystack)) return FALSE;
echo __FUNCTION__ . " ENTRY ****************************************************************<br />";
echo "Needle: " . print_r($needle, TRUE) . "<br />Haystack: " . print_r($haystack, TRUE) . "<br />";
foreach ($needle as $needle_key => $needle_value) {
echo "Analyzing needle pair: " . print_r(array($needle_key => $needle_value), TRUE) . "<br />";
echo "and haystack: " . print_r($haystack, TRUE) . "<br />";// . print_r($needle_value, true) . "<br />";
if (array_key_exists($needle_key, $haystack) && (gettype($needle_value) == gettype($haystack[$needle_key]))) {
// Keys matches and values type matches
echo "Keys matches and values type matches<br />";
if (is_array($needle_value)) {
// Values are arrays - going deeper
echo "Values are arrays - going deeper<br />";
if (!is_subarray($needle_value, $haystack[$needle_key])) return FALSE;
} else if ($needle_value != $haystack[$needle_key]) {
// Values don't match - this is the end
echo "Values don't match - this is the end<br />";
return FALSE;
} else {
// Values do match
echo "Values do match<br />";
//$result = true;
}
} else {
// No key found - this is the end
echo "No key found - this is the end<br />";
return FALSE;
}
}
echo __FUNCTION__ . "**************************************************************** EXIT<br />";
return TRUE;
}
echo "<br />Match: " . ((is_subarray($b, $a)) ? "YES" : "NO");
?>
</pre>