1

I have:

Array1: ([0] => "Lion 1" [1] => "Cat 1" [2] => "Tiger 1" [3] => "Leopard 1")

Array2: ([0] => "Lion 2" [1] => "Dog 2" [2] => "Tiger 2" [3] => "Leopard 2")

Output to show:

            Array1    |   Array2
            ++++++++++++++++++++
            Lion 1    |   Lion 2
            Cat 1     |   ______ <-Blank row
            Tiger 1   |   Tiger 2
            Leopard 1 |   Leopard 2
Blank row -> _______   |   Dog 2
3
  • Which criteria are you using to determine a valid match between elements in array 1 and 2? Following your example my guess is that if the first word is the same, then it's a match. Is this correct? Commented Oct 15, 2012 at 3:24
  • 1
    @jordan arseno thanks for the edit. Commented Oct 15, 2012 at 4:49
  • Relatable: Compare two arrays and present intersections/differences in a github-style Commented Nov 19, 2024 at 8:22

2 Answers 2

1

Here's some code that might be able to help you:

<pre>
<?php
$array1 = Array("Lion 1", "Cat 1", "Tiger 1", "Leopard 1");
$array2 = Array("Lion 2", "Dog 2", "Tiger 2", "Leopard 2");

function build_map($array) {
    $map = Array();

    foreach($array as $val) {
        $parts = explode(' ', $val);
        $map[$parts[0]] = $val;
    }

    return $map;
}

$map1 = build_map($array1);
$map2 = build_map($array2);

$only1 = array_diff_key($map1, $map2);
$only2 = array_diff_key($map2, $map1);
$both = array_intersect_key($map1, $map2);

foreach($both as $key => $val)
    echo $map1[$key]."\t\t".$map2[$key]."\n";

foreach($only1 as $key => $val)
    echo "$val\t\t________\n";

foreach($only2 as $key => $val)
    echo "________\t\t$val\n";

?>
</pre>

Its output is the following.

Lion 1      Lion 2
Tiger 1     Tiger 2
Leopard 1       Leopard 2
Cat 1       ________
________        Dog 2

As long as you have one of each animal in each array, this should work.

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

6 Comments

this is most likely my logic. my problem is i want also to show the value of array2 even does not exist in array1.
Are the two arrays always aligned or can the elements in each array be distributed in any order?
I noticed you edited the output for the code above but it actually doesn't result in that output (the _______ Dog 2 row is not really shown).
each can be distributed in any order.
I just edited the code in order to take into account the requirement of showing all elements in both arrays. Hope it helps.
|
1

Check out str_word_count, array_unique and array_intersect for further details.

You may be able to use them like this:

$array1 = Array(0 => "Lion 1", 1 => "Cat 1", 2 => "Tiger 1", 3 => "Leopard 1");
$array2 = Array(0 => "Lion 2", 1 => "Dog 2", 2 => "Tiger 2", 3 => "Leopard 2");

$animals1 = array_unique(str_word_count(implode(' ', $array1), 1));
$animals2 = array_unique(str_word_count(implode(' ', $array2), 1));

# Compare the two arrays.
$intersect = array_intersect($animals1, $animals2);

# Check if there's a match.
if (count($intersect)) {
   # found a match
}

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.