2

i have 2 queries, and i get back some data by doing a whyle loop.

from the first query if i do print_r($final_key);i get:

hey1
hey2
hey3

from the second one, if i do print_r($final_key2); i get:

hey1 test
hey2 test1
hey3

what i am trying to do is to compare the 2 arrays and check for words that match and i can't do it directly from the database

any ideas?

thanks

edit: here is my query 1:

while ($keywords = mysql_fetch_array($keys1, MYSQL_ASSOC)){
    foreach ($keywords as $key) {
        $plus = '+';
        $pos = strripos($key, $plus);
            if ($pos === false) { } else { 
                $clean_plus = preg_replace("/[\+]/", '', $key);
                $final_key = str_replace("'", "", $clean_plus);
                print_r($final_key);
                echo '<br>';
            }


    }
} 

and the second one:

 <?php
 while ($keywords = mysql_fetch_array($keys)){
if($keywords['kword'] != ''){
    echo $keywords['kword'];
 } } ?>

i am tryingt o match $final_key against $keywords['kword'];

4
  • Could you give us a sample of the two arrays? ie: array("one"=>1, "two", "three") Commented Aug 19, 2011 at 16:39
  • So you want a function that returns 'hey3' because that's the only word that is exactly the same in both arrays? Commented Aug 19, 2011 at 16:40
  • You really can't just do a DB join? Commented Aug 19, 2011 at 16:43
  • please see my edits. the join is out of question Commented Aug 19, 2011 at 16:47

3 Answers 3

5

Use array_intersect:

array_intersect($array1, $array2, ...);

from the array_intersect documentation:

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Sign up to request clarification or add additional context in comments.

2 Comments

i get arning: array_diff() [function.array-diff]: Argument #2 is not an array
array_intersect works on arrays; if you are trying to see if a string exists in an array, perhaps array_search would be a better function to use, but your question is unclear on what you're comparing if that's the case.
2

Use array_intersect which returns an array containing values in both arrays.

If you want the keys to match, use array_intersect_assoc.

4 Comments

i get arning: array_diff() [function.array-diff]: Argument #2 is not an array
You need to be more clear about what your variable types are. You start the question by saying you have two arrays. By the end of your edits, it looks like you're comparing two strings. Which is it?
sorry, i guess they are strings
If they're both strings, then why not standard compare ==? If one is an array and the other is a string, you could use array_search. If you're looking to compare substrings in two strings separated by spaces (or another delimiter), you can use explode to get each substring as an array element. Then use array_intersect. If this is what you're trying to do, you should edit your question because it definitely doesn't look like that right now.
0

Loop over one array, and on each iteration, loop the other.

<?php
foreach($array as $item){
    foreach($array2 as $item2){
        if($item == $item2){
            $array3[] = $item;
        }
    }
}
?>

This will create an array of matches, which you can then do with as you will.

1 Comment

Looks like root45 has the solution... Oops.

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.