使用相同的键合并数组

时间:2011-05-04 09:39:51

标签: php array-merge

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

问题:

 $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

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

那么如何将所有与相同的键合并?

7 个答案:

答案 0 :(得分:48)

您需要使用array_merge_recursive代替array_merge。当然,数组中只能有一个等于'c'的键,但关联的值将是包含34的数组。

See it in action

答案 1 :(得分:23)

尝试使用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>";

将返回

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

    [d] => 5
)

答案 2 :(得分:4)

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

如果要将此数组转换为以下内容:

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

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

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

希望这个答案能在某个时候帮助某人。

答案 3 :(得分:2)

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

答案 4 :(得分:2)

我刚写了这个函数,它应该为你做的伎俩,但它确实留下了连接

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

答案 5 :(得分:1)

数组中的两个条目无法共享密钥,您需要更改重复的密钥

答案 6 :(得分:1)

试试这个:

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