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): - $results = array($question1,$question2,$question3);
SECOND ARRAY (MULTI-DIMENSIONAL) - $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... - if (in_array($results, $baseball)) {
-
print "WE HAVE A MATCH";
-
} else {
-
print "NO MATCH";
-
}
But have had no luck :(
4 2973
current idea: check each $baseball array against $result using count() & array_diff().
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!
ok.
check #1 if the arrays differ in length, they can not match. - 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 = ∅) - // do for each relevant array
-
if (array_diff($baseball[0], $result)) // fail
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 :) -
## 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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
|
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...
|
by: Philip Wagenaar |
last post by:
what is the quickest way to check if an array is multidimensional
|
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
...
|
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?).
...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |