2

I am using this piece of code to compare two php arrays, I got all matched items rightly but unfortunately i am not succeeding in getting non matched items. Here is my code.

<?php 

$filter1 = "blueberries,abc,cornstarch,sugar";
$cval = strtoupper($filter1);         
$parts1 = explode(',',$cval);

$filter2 = "water,blueberries,sugar,cornstarch";
$values = strtoupper($filter2);
$parts2 = explode(',', $values);


$m=0;
$nm=0;


for($i = 0; $i< count($parts1); $i++)
{

for($j = 0; $j< count($parts2); $j++)
{

if($parts1[$i] == $parts2[$j])
{
$m++;
$p[] = $parts1[$i];
}
else{
$nm++;
$q[] = $parts1[$i];
}

}

}

echo $m;
echo "<br />";
echo $nm;
echo "<br />";
print_r($p);
echo "<br />";
print_r($q);

?>

Can someone help me sorting out this problem, As i dont want to use builtin array comparison functions.

4
  • 2
    use array_interesect and array_diff Commented Dec 6, 2012 at 10:57
  • 1
    Sorry i want to use loops thanks. Commented Dec 6, 2012 at 10:58
  • 1
    Which raises the question, why do you want to use loops rather than the built-in functions? Commented Dec 6, 2012 at 11:09
  • This is absolutely ridiculous, once you compare the arrays you will need to do the for loops all over again to compare the other array. Stop fighting the inbuilt functions Commented Dec 6, 2012 at 11:33

4 Answers 4

2

I would use the internal functions for this (if I understood your question)

Matched:

$matched = array_intersect($parts1, $parts2);

Difference:

$difference = array_diff($parts1, $parts2);

Or in your world of for loops:

<?php
    $filter1 = "blueberries,abc,cornstarch,sugar";
    $cval = strtoupper($filter1);
    $parts1 = explode(',', $cval);

    $filter2 = "water,blueberries,sugar,cornstarch";
    $values = strtoupper($filter2);
    $parts2 = explode(',', $values);

    $matched = $not_matched = array();

    for ($i = 0; $i < count($parts1); $i++)
    {

        if (in_array($parts1[$i], $parts2))
        {
            $matched[] = $parts1[$i];
        }
        else
        {
            $not_matched[] = $parts1[$i];
        }

    }

    print_r($matched);
    print_r($not_matched);
?>

I have been working on adding another loop instead of using in_array() but I'm not going to update my answer for two reasons.

1) Use

$matched = array_intersect($parts1, $parts2);
$difference = array_diff($parts1, $parts2);

Writing this much code instead of two lines is against my religion.

2) The only reason to create this amount of code stricly not using array_* or in_array functions must be for some quiz or test, which I want to enter and win.

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

3 Comments

I guess in_array() is not allowed
@Dale ahh...please use one more loop ...instead in_array() ...:P
@NullPointer I have my reasons for quitting :)
1

Use array_intersect, array_diff and array_merge

$filter1 = "blueberries,abc,cornstarch,sugar";
$cval = strtoupper($filter1);         
$parts1 = explode(',',$cval);

$filter2 = "water,blueberries,sugar,cornstarch";
$values = strtoupper($filter2);
$parts2 = explode(',', $values);

print_r(array_intersect($parts1, $parts2));

print_r(array_merge(array_diff($parts1, $parts2), array_diff($parts2, $parts1)));

Array ( [0] => BLUEBERRIES [2] => CORNSTARCH [3] => SUGAR ) Array ( [0] => ABC [1] => WATER )

Comments

1

You are doing the cartesian product of the two arrays. If you find that $parts1[$i] == $parts2[$j], it is correct to add $parts1[$i] to $p, but the opposite assumption (if they are different then add to $q) is wrong. You should start with all elements in $q and when you have a match, add it to $p and remove it from $q.

Comments

1
    <?php
    $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);

    print_r($result);
    ?>

This is the php library function.

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.