473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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....Anyways


FIRST ARRAY (SINGLE):
Expand|Select|Wrap|Line Numbers
  1. $results = array($question1,$question2,$question3);
SECOND ARRAY (MULTI-DIMENSIONAL)
Expand|Select|Wrap|Line Numbers
  1. $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...


Expand|Select|Wrap|Line Numbers
  1.    if (in_array($results, $baseball)) {
  2.       print "WE HAVE A MATCH";
  3.    } else {
  4.       print "NO MATCH";
  5.    }

But have had no luck :(
Nov 10 '09 #1
4 2973
Dormilich
8,658 Expert Mod 8TB
current idea: check each $baseball array against $result using count() & array_diff().
Nov 10 '09 #2
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!
Nov 10 '09 #3
Dormilich
8,658 Expert Mod 8TB
ok.
check #1 if the arrays differ in length, they can not match.
Expand|Select|Wrap|Line Numbers
  1. if (3 < count($result)) // fail
  2. if (2 > count($result)) // fail
check #2 if the arrays correspond in length, the difference is empty for equal arrays (∀ B = A : A \ B = ∅)
Expand|Select|Wrap|Line Numbers
  1. // do for each relevant array
  2. if (array_diff($baseball[0], $result)) // fail
Nov 10 '09 #4
dlite922
1,584 Expert 1GB
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 :)

Expand|Select|Wrap|Line Numbers
  1. ## get the answers from the user
  2. $question1 = 'male'; 
  3. $question2 = 'middle';
  4. $question3 = 'short';
  5. $results = array($question1,$question2,$question3);
  6.  
  7. ## source values
  8. $baseball = array( array("male","female"), array("right","middle"), array("long","medium","short"));
  9.  
  10. // First make sure you have minimum values
  11. // Here I assume all three questions are required (ie we must have a value for each inner array)
  12. $resultSize = count($results); 
  13. $sourceSize = count($baseball); 
  14. $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
  15.  
  16. // if number of answers match
  17. if($resultSize == $sourceSize) {
  18.  
  19.     // 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)
  20.     for($i = 0;$i < $resultSize; $i++) 
  21.     {
  22.         if(!in_array($results[$i],$baseball[$i])) // if answer not in the array, fail it! 
  23.         { 
  24.             $validation = 'fail';     
  25.             // optional:
  26.             break; // you can leave once one validation fails, no reason to check the rest. 
  27.         }
  28.     }
  29.  
  30.     // optional: 
  31.     if($validation != 'fail')
  32.     {
  33.         $validation = 'pass'; 
  34.     }
  35.  
  36. } 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. 
  37. {
  38.     die("Error: One or more questions were not answered or more answer values then there are questions.");
  39. }
  40.  
  41. echo $validation; 
  42.  
This should now behave with your test cases in the OP. Change middle to LEFT and you'll get a fail message.

Cheers,



Dan
Nov 10 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
20
by: Pavel Stehule | last post by:
Hello, Is possible merge two arrays like array + array => array select array_append(array, array); ERROR: function array_append(integer, integer) does not exist
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
5
by: Philip Wagenaar | last post by:
what is the quickest way to check if an array is multidimensional
14
by: frizzle | last post by:
Hi there, I have a string, comma separated, with links and their respective URLs in it. Example: Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org ...
2
by: =?Utf-8?B?SmVucw==?= | last post by:
Hi, I have a multidimensional Array (2 dimensions). I'd like to hand that over to another function (respective another thread in another class, but that should be the same, shouldn't it?). ...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
4
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.