0

I'm looking for niftier check if multidim. array $a matches pattern (key/value) in multidim. array $b (or in other words, check if $b is a subarray of $a). My recursive traversal code with trace help attached below, ready to paste in an empty script.

This is needed to filter mdim array of products using mdim array of its chosen properties to match.

$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");
3
  • possible duplicate of : stackoverflow.com/questions/145337/… Commented Dec 9, 2015 at 14:42
  • Recursive traversing Commented Dec 9, 2015 at 15:02
  • Maybe... At the beginning I was asking for help, but now, when I helped myself, I should close I think... Thanks! Commented Dec 10, 2015 at 10:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.