php 合并具有相同键的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5881443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:46:42  来源:igfitidea点击:

Merging arrays with the same keys

phparray-merge

提问by beytarovski

In a piece of software, I merge two arrays with array_mergefunction. But I need to add the same array (with the same keys, of course) to an existing array.

在一个软件中,我用array_merge函数合并了两个数组。但我需要将相同的数组(当然,使用相同的键)添加到现有数组中。

The problem:

问题:

 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);

 array_merge($A, $B);

 // result
 [a] => 1 [b] => 2 [c] => 4 [d] => 5

As you see, 'c' => 3is missed.

如你所见,'c' => 3错过了。

So how can I merge all of themwith the same keys?

那么我怎样才能用相同的键合并所有这些呢?

回答by Jon

You need to use array_merge_recursiveinstead of array_merge. Of course there can only be one key equal to 'c'in the array, but the associated value will be an array containing both 3and 4.

您需要使用array_merge_recursive而不是array_merge. 当然'c',数组中只能有一个等于的键,但关联的值将是一个同时包含3和的数组4

回答by diEcho

Try with array_merge_recursive

尝试使用array_merge_recursive

$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);

echo "<pre>";
print_r($c);
echo "</pre>";

will return

将返回

Array
(
    [a] => 1
    [b] => 2
    [c] => Array
        (
            [0] => 3
            [1] => 4
        )

    [d] => 5
)

回答by Melon

$arr1 = array(
   "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
   "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
   "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
   "3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
   "4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);

if you want to convert this array as following:

如果你想转换这个数组如下:

$arr2 = array(
   "0" => array(
          "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
          "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
          "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
          "3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
    ),

    "1" => array(
          "0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
     )
);

so, my answer will be like this:

所以,我的回答是这样的:

$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
    $inner_array = array();

    $fid_value = $value['fid'];
    if(!in_array($value['fid'], $unique_array))
    {
            array_push($unique_array, $fid_value);
            unset($value['fid']);
            array_push($inner_array, $value);
            $outer_array[$fid_value] = $inner_array;


    }else{
            unset($value['fid']);
            array_push($outer_array[$fid_value], $value);

    }
}
var_dump(array_values($outer_array));

hope this answer will help somebody sometime.

希望这个答案会在某个时候对某人有所帮助。

回答by Kamaro Lambert

I just wrote this function, it should do the trick for you, but it does left join

我刚刚写了这个函数,它应该可以为你解决问题,但它确实 left join

public function mergePerKey($array1,$array2)
    {
        $mergedArray = [];

        foreach ($array1 as $key => $value) 
        {
            if(isset($array2[$key]))
             {
               $mergedArray[$value] = null;

               continue;
             }
            $mergedArray[$value] = $array2[$key];
        }

        return $mergedArray;
    }

回答by Nemoden

 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);
 $C = array_merge_recursive($A, $B);
 $aWhere = array();
 foreach ($C as $k=>$v) {

    if (is_array($v)) {
        $aWhere[] = $k . ' in ('.implode(', ',$v).')';
    }
    else {
        $aWhere[] = $k . ' = ' . $v;
    }
 }
 $where = implode(' AND ', $aWhere);
 echo $where;

回答by Mild Fuzz

Two entries in an array can't share a key, you'll need to change the key for the duplicate

数组中的两个条目不能共享一个键,您需要更改重复项的键

回答by Radhason

Try this:

尝试这个:

$array_one = $_POST['ratio_type'];
$array_two = $_POST['ratio_type'];
$array_three = $_POST['ratio_type'];
$array_four = $_POST['ratio_type'];
$array_five = $_POST['ratio_type'];
$array_six = $_POST['ratio_type'];
$array_seven = $_POST['ratio_type'];
$array_eight = $_POST['ratio_type'];

$all_array_elements = array_merge_recursive(
    $array_one,
    $array_two,
    $array_three,
    $aray_four, 
    $array_five,
    $array_six,
    $array_seven,
    $array_eight
);

print_r($all_array_elements, 1);

foreach ($all_array_elements as $key => $value) {          
    echo $key.'-'.$value;            
}