Php 比较2个数组中的2个数组值
我删除了那篇旧帖子,以便更清楚地说明这一点。我有2个数组需要比较和匹配,但前提是每个数组的2个值相同Php 比较2个数组中的2个数组值,php,arrays,compare,Php,Arrays,Compare,我删除了那篇旧帖子,以便更清楚地说明这一点。我有2个数组需要比较和匹配,但前提是每个数组的2个值相同 $array1=$plugins $array2=$xml\u dump 两个阵列的外观示例: $plugins Array ( [all] => Array ( [ajax-category-dropdown/dhat-ajax-cat-dropdown.php] => Array (
$array1=$plugins
$array2=$xml\u 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
)
仅当$array1Name
,Version
与$array2Name
,ex\u 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
我尝试了许多不同的
数组_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);
如果这不起作用,我建议您放弃现实世界中的阵列,转而创建一些简单/小型的虚拟阵列,并向我们提供您想要为相同的虚拟数据存档的结果。
<?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
)
上述示例将输出:
排列
(
[a] =>绿色
[0]=>红色
)
我只是想用我的解决方案来结束这一切 我为两个数组获取
名称
和版本
,并为每个数组创建一个新数组,这样我就可以轻松地使用数组_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);
array_intersect是您可能需要的,这有点神秘……是的,这是一种神秘的方式,我完全更改了它,希望它更清晰。类似于这样,但这返回0,即使array_intersect各自工作并返回一个值。@Wyck:也许您需要使用
array_intersect()
而不是array\u intersect\u assoc(),我想我可以制作一个函数并使用array\u uintersect