【问题标题】:Compare 2 array values in 2 arrays Php比较 2 个数组 Php 中的 2 个数组值
【发布时间】:2011-04-28 15:38:42
【问题描述】:

我已删除旧帖子以使这一点更清楚。我有 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
                )

仅当 $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 是你可能需要的
  • 这有点神秘……
  • 是的,这是一种神秘的方式,我完全改变了它,所以希望它更清楚。

标签: php arrays compare


【解决方案1】:

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

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

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

【讨论】:

  • 类似这样的东西,但是即使 array_intersect 都单独工作并返回一个值,它也会返回 0。
  • @Wyck:也许您需要使用array_intersect() 而不是array_intersect_assoc()
  • @Wyck:如果您能分享您的数组内容和预期输出的样本,将会非常有帮助。
  • 我只是尝试array_intersect() 没有骰子,我将数组的转储添加到 OP。
  • 是的,把这些都写出来有帮助,哈哈,我想我可以做一个函数并使用array_uintersect
【解决方案2】:
<?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

【讨论】:

    【解决方案3】:

    只是想用最终成为我的解决方案来结束它。

    我为两个数组获取了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);
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2019-05-22
      • 2020-08-18
      • 2022-01-11
      相关资源
      最近更新 更多