簡體   English   中英

比較 2 arrays Php 中的 2 個數組值

[英]Compare 2 array values in 2 arrays Php

我已經刪除了舊帖子以使這一點更清楚。 我有 2 個 arrays 需要比較和匹配,但前提是每個數組的 2 個值相同。

$array1 = $plugins
$array2 = $xml_dump

arrays 的樣例:

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

僅當 $array1 NameVersion與 $array2 nameex_version匹配時,我才需要它返回值(或返回 true)

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

$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 個值。

如果我理解正確,應該這樣做:

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

如果這不起作用,我建議您放棄您的真實世界 arrays 並創建一些簡單/小型的虛擬對象,並為我們提供您想要為相同的虛擬數據存檔的結果。

<?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

只是想用最終成為我的解決方案來結束這個。

我獲取了 arrays 的NameVersion ,並為每個創建了一個新數組,這樣我就可以輕松地使用array_intersect並操作它們 arrays。 所以為了讓它工作,我只需要創建兩個新的 arrays 和我想要比較的值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM