How to check if a Single Array is part of a Multidimensional Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgendr2
    New Member
    • Nov 2009
    • 2

    How to check if a Single Array is part of a Multidimensional Array?

    So here is my problem

    I do not know if there is another way to solve this without using arrays....but I am assuming that I DO need to use arrays....Anywa ys


    FIRST ARRAY (SINGLE):
    Code:
    $results = array($question1,$question2,$question3);
    SECOND ARRAY (MULTI-DIMENSIONAL)
    Code:
    $baseball = array( array("male","female"), array("right","middle"), array("long","medium","short"));
    What I want to do is see if the values of the Results array fall within the Baseball array.

    For example if $results = ("male","right" ,"long") then we would have a match!

    BUT, if $results = ("male","LEFT", "long") we would NOT have a match....


    So how do I check this? Any help would be greatly appreciated....


    I have tried this method...


    Code:
       if (in_array($results, $baseball)) {
          print "WE HAVE A MATCH";
       } else {
          print "NO MATCH";
       }

    But have had no luck :(
    Last edited by Atli; Nov 10 '09, 11:44 AM. Reason: Added [code] tags.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    current idea: check each $baseball array against $result using count() & array_diff().

    Comment

    • jgendr2
      New Member
      • Nov 2009
      • 2

      #3
      Dormilich,

      Thanks for the reply....I am very new to programming so can you elaborate on how to use the count() and array_diff() here?

      Thank you!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        ok.
        check #1 if the arrays differ in length, they can not match.
        Code:
        if (3 < count($result)) // fail
        if (2 > count($result)) // fail
        check #2 if the arrays correspond in length, the difference is empty for equal arrays (∀ B = A : A \ B = ∅)
        Code:
        // do for each relevant array
        if (array_diff($baseball[0], $result)) // fail

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          I'm not sure Dormi understood the question. Here's how I understood it.

          Code and comments are better than words:

          Note: I do not provide free code, but since you had the values, I just wrote the condition. I decided to give you the code and write the description in the comments. Don't expect this all the time :)

          Code:
          ## get the answers from the user
          $question1 = 'male'; 
          $question2 = 'middle';
          $question3 = 'short';
          $results = array($question1,$question2,$question3);
          
          ## source values
          $baseball = array( array("male","female"), array("right","middle"), array("long","medium","short"));
          
          // First make sure you have minimum values
          // Here I assume all three questions are required (ie we must have a value for each inner array)
          $resultSize = count($results); 
          $sourceSize = count($baseball); 
          $validation = '';  // the initial value for validation. You can set it default to pass and get rid of the line below that sets it to pass
          
          // if number of answers match
          if($resultSize == $sourceSize) {
          
          	// traverse the array (this is why I needed the question and answeres to be the same value, if they weren't you have to compare by hand)
          	for($i = 0;$i < $resultSize; $i++) 
          	{
          		if(!in_array($results[$i],$baseball[$i])) // if answer not in the array, fail it! 
          		{ 
          			$validation = 'fail'; 	
          			// optional:
          			break; // you can leave once one validation fails, no reason to check the rest. 
          		}
          	}
          	
          	// optional: 
          	if($validation != 'fail')
          	{
          		$validation = 'pass'; 
          	}
          
          } else // answers do not match, exit or try to compare the values to each baseball field if you know what index the inner arrays fall on. 
          {
          	die("Error: One or more questions were not answered or more answer values then there are questions.");
          }
          
          echo $validation;
          This should now behave with your test cases in the OP. Change middle to LEFT and you'll get a fail message.

          Cheers,



          Dan

          Comment

          Working...