5

I have deleted the old post to make this more clear. I have 2 arrays that I need to compare and match but only if 2 values per array are the same.

$array1 = $plugins
$array2 = $xml_dump

A sample of what both arrays look like:

$plugins 

Array
(
    [all] => Array
        (
            [ajax-category-dropdown/dhat-ajax-cat-dropdown.php] => Array
                (
                    [Name] => Ajax Category Dropdown
                    [PluginURI] => http://www.example.com/ajax/
                    [Version] => 0.1.5
                    [Description] => Generates multi-level ajax. 
                    [Author] => DyasonHat
                    [AuthorURI] => http://www.dyasonhat.com
                    [Title] => Ajax Category Dropdown
                    [AuthorName] => Dya
                )

            [akismet/akismet.php] => Array
                (
                    [Name] => Akismet
                    [PluginURI] => http://akismet.com/
                    [Version] => 2.5.3
                    [Description] => Used by millions
                    [Author] => Automattic
                    [AuthorURI] => http://automattic.com/
                    [Title] => Akismet
                    [AuthorName] => Automattic
                )


$xml_dump
SimpleXMLElement Object
(
    [plugin] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Ajax Category Dropdown
                    [ex_version] => 0.1.5
                    [ex_date] => 2008-01-03
                    [plugin_url] => http://wordpress.org/extend/plugins/wp-contactform/
                    [advisory_url] => http://osvdb.org/show/osvdb/43284
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => WP-ContactForm
                    [ex_version] => 2.0.7
                    [ex_date] => 2008-01-03
                    [plugin_url] => http://wordpress.org/extend/plugins/wp-contactform/
                    [advisory_url] => http://osvdb.org/show/osvdb/43284
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => Math Comment Spam Protection
                    [ex_version] => 2.1
                    [ex_date] => 2008-01-03
                    [plugin_url] => http://wordpress.org/extend/plugins/math-comment-spam-protection/
                    [advisory_url] => a
                )

I need it to return the value ( or return true) only if $array1 Name , Version matches $array2 name ,ex_version

In the example above you can see that

$array1
Name => Ajax Category Dropdown
Version => 0.1.5

///has a match in 

$array2
name => Ajax Category Dropdown
ex_version => 0.1.5

I have tried many variations of array_intersect but I cannot get it to match 2 values for each array.

3
  • array_intersect is what you probably need Commented Apr 28, 2011 at 15:43
  • 3
    This is a bit cryptic... Commented Apr 28, 2011 at 15:44
  • 1
    Yes it was way to cryptic, I completely changed it so hopefully it is more clear. Commented Apr 28, 2011 at 18:56

3 Answers 3

2

If I understood you right this should do it:

array_intersect_assoc(array_intersect($global_plugins, $xml_plugins), array_intersect($plugin_verso, $xml_plugin_version));

Your question is very confusing, you are talking about $array1 and $array2 but I see four of them!

Anyway, have you tried the following? This is just a wild guess, but...

$result = array_intersect($global_plugins, $xml_plugins, $xml_plugin_version, $plugin_verso);

If this doesn't work, I suggest you drop your real world arrays and create some simple / small dummy ones instead and provide us with the result you want to archive for the very same dummy data.

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

15 Comments

Something like this, but this returns 0 even though both array_intersect work on their own and return a value.
@Wyck: Perhaps you need to use array_intersect() instead of array_intersect_assoc()?
@Wyck: It would be very helpful if you could share a sample of your array contents and the expected output.
I tried just array_intersect() no dice, I added a dump of the array to OP.
Yes writing this all out helped lol, I think I might make a function and use array_uintersect
|
1
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?> 
The above example will output:

Array
(
    [a] => green
    [0] => red
)

http://php.net/manual/en/function.array-intersect.php

Comments

0

Just wanted to close this with what ended up being my solution.

I grab the Name and Version for both arrays and created a new array for each one, that way I could easily use array_intersect and manipulate they arrays. So to get this to work I just needed to create two new arrays with the values I wanted to compare.

 $a = array();
 $b = array();

 //first foreach loop

 $find_local_plugs = $global_plugins_name . $plugin_version;
 $a[] = $find_local_plugs;

 //second foreach loop

 $find_remote_plugs = $xml_plugins . $xml_plugin_version;
 $b[] = $find_remote_plugs;


 $matches = array_intersect($a, $b);

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.