Php 比较合并数组
我试图在php中比较和合并数组。假设我有两个数组,如下所示Php 比较合并数组,php,arrays,merge,Php,Arrays,Merge,我试图在php中比较和合并数组。假设我有两个数组,如下所示 $topCountries1 = array( array( 'India', '23', '34', '11'), array( 'USA', '13', '24', '21'), array( 'Japan', '13', '24',
$topCountries1 = array(
array(
'India',
'23',
'34',
'11'),
array(
'USA',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
$topCountries2 = array(
array(
'France',
'23',
'34',
'11'),
array(
'India',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
我想合并上面两个数组,这样我就有了国家的唯一值集,如果数组中有重复的国家,它应该添加其他三个字段的值并合并
尝试以下代码-但我对逻辑感到困惑
$topCountries = array_merge($topCountries1, $topCountries2);
$collect = array();
foreach ($topCountries as $tc) {
echo $count = count($collect);
if ($count > 0) {
foreach ($collect as $c) {
if ($c[0] == $tc[0]) {
echo "match<br/>";
$collect[] = $tc;
} else {
$collect[] = $tc;
echo "no match<br/>";
}
}
} else {
$collect[] = $tc;
}
echo "<br/>";
}
$topCountries=array\u merge($topCountries 1,$topCountries 2);
$collect=array();
foreach(顶级国家为$tc){
echo$count=计数($collect);
如果($count>0){
foreach($c收取){
如果($c[0]=$tc[0]){
回显“匹配
”;
$collect[]=$tc;
}否则{
$collect[]=$tc;
回显“不匹配
”;
}
}
}否则{
$collect[]=$tc;
}
回声“
”;
}
我以不同的方式尝试了你的答案,得到了你想要的结果
$topCountries1 = array(
array(
'India',
'23',
'34',
'11'),
array(
'USA',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
$topCountries2 = array(
array(
'France',
'23',
'34',
'11'),
array(
'India',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
$collection = array();
foreach ($topCountries1 as $tc1) {
foreach($topCountries2 as $tc2){
if(in_array($tc1[0], $tc2)){
$collect[0] = $tc1[0];
$collect[1] = $tc1[1] + $tc2[1];
$collect[2] = $tc1[2] + $tc2[2];
$collect[3] = $tc1[3] + $tc2[3];
array_push($collection, $collect);
}
}
}
$final_array = $collection;
foreach($topCountries1 as $tc1){
$flag = true;
foreach($collection as $coll){
if(in_array($tc1[0], $coll)){
$flag = false;
break;
}
}
if($flag){
array_push($final_array, $tc1);
}
}
foreach($topCountries2 as $tc1){
$flag = true;
foreach($collection as $coll){
if(in_array($tc1[0], $coll)){
$flag = false;
break;
}
}
if($flag){
array_push($final_array, $tc1);
}
}
var_dump($final_array);
数组\u merge将合并所需的数据添加数据我以不同的方式尝试了您的答案,得到了您想要的结果
$topCountries1 = array(
array(
'India',
'23',
'34',
'11'),
array(
'USA',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
$topCountries2 = array(
array(
'France',
'23',
'34',
'11'),
array(
'India',
'13',
'24',
'21'),
array(
'Japan',
'13',
'24',
'21'));
$collection = array();
foreach ($topCountries1 as $tc1) {
foreach($topCountries2 as $tc2){
if(in_array($tc1[0], $tc2)){
$collect[0] = $tc1[0];
$collect[1] = $tc1[1] + $tc2[1];
$collect[2] = $tc1[2] + $tc2[2];
$collect[3] = $tc1[3] + $tc2[3];
array_push($collection, $collect);
}
}
}
$final_array = $collection;
foreach($topCountries1 as $tc1){
$flag = true;
foreach($collection as $coll){
if(in_array($tc1[0], $coll)){
$flag = false;
break;
}
}
if($flag){
array_push($final_array, $tc1);
}
}
foreach($topCountries2 as $tc1){
$flag = true;
foreach($collection as $coll){
if(in_array($tc1[0], $coll)){
$flag = false;
break;
}
}
if($flag){
array_push($final_array, $tc1);
}
}
var_dump($final_array);
array\u merge将合并所需的数据添加数据您只需按要合并的键组织一个全局数组即可,这样您就不必以O(n²)的复杂度进行操作,只需简单的O(n)假设键的哈希查找为O(1) 这是一个比以前发布的解决方案更简单的解决方案,它可以接受任意数量的输入数组,而无需添加更多代码,此外,还允许您扩展国家名称后的值数量,而无需添加更多代码
$topCountries1 = array(
array(
'India',
'23',
'34',
'11',
),
array(
'USA',
'13',
'24',
'21',
),
array(
'Japan',
'13',
'24',
'21',
),
);
$topCountries2 = array(
array(
'France',
'23',
'34',
'11',
),
array(
'India',
'13',
'24',
'21',
),
array(
'Japan',
'13',
'24',
'21',
),
);
$countries = array();
$data = array($topCountries1, $topCountries2);
foreach($data as $entries)
{
foreach($entries as $country)
{
$name = $country[0];
// if first time we see the country, add it to the list
if (!isset($countries[$name]))
{
$countries[$name] = $country;
}
else
{
// add all fields after the first (the name)
foreach (array_slice($country, 1, null, true) as $idx => $value)
{
$countries[$name][$idx] += $value;
}
}
}
}
var_dump($countries);
var_dump(array_values($countries));
希望有帮助 只需保持一个按要合并的键组织的全局数组,就可以做到这一点,而且不必使用O(n²)复杂度,只需简单的O(n)假设该键的哈希查找为O(1) 这是一个比以前发布的解决方案更简单的解决方案,它可以接受任意数量的输入数组,而无需添加更多代码,此外,还允许您扩展国家名称后的值数量,而无需添加更多代码
$topCountries1 = array(
array(
'India',
'23',
'34',
'11',
),
array(
'USA',
'13',
'24',
'21',
),
array(
'Japan',
'13',
'24',
'21',
),
);
$topCountries2 = array(
array(
'France',
'23',
'34',
'11',
),
array(
'India',
'13',
'24',
'21',
),
array(
'Japan',
'13',
'24',
'21',
),
);
$countries = array();
$data = array($topCountries1, $topCountries2);
foreach($data as $entries)
{
foreach($entries as $country)
{
$name = $country[0];
// if first time we see the country, add it to the list
if (!isset($countries[$name]))
{
$countries[$name] = $country;
}
else
{
// add all fields after the first (the name)
foreach (array_slice($country, 1, null, true) as $idx => $value)
{
$countries[$name][$idx] += $value;
}
}
}
}
var_dump($countries);
var_dump(array_values($countries));
希望有帮助 非常感谢。它很有魅力。现在我知道我做错了什么,并且使整个过程复杂化了,因为我有一个键值为以下文本的数组。(int)0=>object(stdClass){country=>'India'visions=>'12'newvisions=>'9'percentnewvisions=>(int)75},我还可以使用相同的吗?因为我们使用它作为键值对?是的,代码不对用于描述数组中其他值的键进行假设。唯一的假设是国家是在数组中首先定义的。如果对值使用关联数组,则可以进一步简化代码(删除数组_slice()并取消设置()国家名称)。您需要更改$name=。。使用你的关联值。抱歉,没有得到下拉拼接部分。非常感谢。它很有魅力。现在我知道我做错了什么,并且使整个过程复杂化了,因为我有一个键值为以下文本的数组。(int)0=>object(stdClass){country=>'India'visions=>'12'newvisions=>'9'percentnewvisions=>(int)75},我还可以使用相同的吗?因为我们使用它作为键值对?是的,代码不对用于描述数组中其他值的键进行假设。唯一的假设是国家是在数组中首先定义的。如果对值使用关联数组,则可以进一步简化代码(删除数组_slice()并取消设置()国家名称)。您需要更改$name=。。但是,使用关联值。抱歉,没有获得下拉拼接部分。非常感谢。该死的,我忘了阵列推:我真是太感谢你了。该死的,我忘了阵列了