比较2个数组中的2个数组值Php

时间:2011-04-28 15:38:42

标签: php arrays compare

我删除了旧帖子以使其更清晰。我有2个数组,我需要比较和匹配,但只有每个数组2个值相同。

$array1 = $plugins
$array2 = $xml_dump

两个数组的样本:

$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
                )

我需要它才能返回值(或返回true)仅当$ array1 NameVersion匹配$ array2 nameex_version

在上面的示例中,您可以看到

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

///has a match in 

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

我尝试了array_intersect的许多变体,但我不能让它匹配每个数组的2个值。

3 个答案:

答案 0 :(得分:2)

如果我理解你的话,应该这样做:

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

您的问题非常混乱,您在谈论的是$array1$array2,但我看到了其中的四个!

无论如何,你试过以下吗?这只是一个疯狂的猜测,但是......

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

如果这不起作用,我建议您删除现实世界的数组并创建一些简单/小的虚拟数据,并为我们提供您想要存档的相同虚拟数据的结果。

答案 1 :(得分: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

答案 2 :(得分:0)

只是想结束这个以最终成为我的解决方案。

我抓住两个数组的NameVersion并为每个数组创建一个新数组,这样我就可以轻松地使用array_intersect并操纵它们的数组。因此,为了实现这一点,我只需要创建两个具有我想要比较的值的新数组。

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