I have data stored in a string: such as $string='12';
'12' represents 2 pieces of data, 1
and 2
In 3 columns, I have those same ids stored, the array comes out as :
ex1: $array= ('num1' => 1, 'num2' => 2, 'num3' => )
ex2: $array= ('num1' => 5, 'num2' => 4, 'num3' => 3)
How could I compare these and return true if the numbers exist in both places?
For instance: using the above example: for ex1: 12
would return TRUE
34
would return FALSE
for ex2: 345
would return TRUE
This works, please see results below: (Value must be present in both array to return true.
$result = true;
$string1 = '43';
$string = str_split($string1);
$example1 = array('num1' => 1, 'num2' => 2, 'num3' => 3, 'num4' => 4, 'num5' => 5);
$example2 = array('num1' => 5, 'num2' => 4, 'num3' => 3);
foreach ($string as $st) {
if((in_array($st, $example1) && in_array($st, $example2)) && $result == true){
$result = true; //true
} else {
$result = false;
}
}
if($result == true){
echo 1; //true
} else {
echo 0; //false
}
exit;
//Test Results:
//$string1 = '12'; //result 0
//$string1 = '34'; //result 1
//$string1 = '55'; //result 1
//$string1 = '43'; //result 1
try this: You can use array_intersect().
$string = '12';
//split string into array of characters
$arr = str_split($string);
//testing columns
$compare1 = array('num1' => 1, 'num2' => 2, 'num3' => '');
$compare2 = array('num1' => 5, 'num2' => 4, 'num3' => 3);
//returns an array containing all the values of $arr inside $compare1
$int1 = array_intersect($arr, $compare1);
//returns an array containing all the values of $arr inside $compare2
$int2 = array_intersect($arr, $compare2);
//if $arr contents are in $compare1 return true, otherwise false
echo ! empty($int1);
//if $arr contents are in $compare2 return true, otherwise false
echo ! empty($int2);
try in_array() function http://php.net/manual/en/function.in-array.php
but I don't understand why 345 would return true
I'm late to the party... but I had already started, so might as well! Here's my solution:
function yayOrNay($array, $string) {
$stringArray = str_split($string);
$arrayDiff = array_diff($stringArray, array_values($array));
return empty($arrayDiff);
}