1

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

2
  • Why does 34 return FALSE? Commented Feb 28, 2013 at 19:33
  • 12 and 34 are for example 1 and 345 is for example2 Commented Feb 28, 2013 at 19:34

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

try in_array() function http://php.net/manual/en/function.in-array.php

but I don't understand why 345 would return true

7 Comments

because array2 contains 3, 4 and 5
yeah i apologize. IT was written confusing. The first 2 return examples were directed at example 1. and the last was directed at example 2
true, but he said if $string exists in both places
in_array wouldnt work by the way since what he is trying to search is string and not array. Unless you convert it to array
he has an array, and a string to search for. yes it will work. not as nice as your intersect method, but yes it will. split his original string into parts and check in_array for each part...
|
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);

Comments

1

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);
}

LIVE DEMO

Comments

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.