在php中合并两个assosiative数组?

时间:2022-10-24 22:20:04

I have two associative arrays.

我有两个关联数组。

array ( [apple]=>1 [banana]=>2 cocunet => 3)

and other array is

和其他数组是

array ( [apple]=>2 [banana]=>3 cocunet => 4)

now i want to merge my array like that

现在我想合并我的数组

array ( [apple]=>1,2 [banana]=>2,3 cocunet => 3,4)

3 个解决方案

#1


6  

There is no such array in PHP. The thing you want can only be done by creating multidimentional arrays.

PHP中没有这样的数组。你想要的东西只能通过创建多维数组来完成。

$a1 = array( 'apple'=>1,'banana'=>2,'coconut'=> 3);

$a2 = array( 'apple'=>2,'banana'=>3,'coconut'=> 4);

echo "<pre>";
print_r(array_merge_recursive($a1,$a2));
echo "</pre>";

For this you can use the array_merge_recursive() function.

为此,您可以使用array_merge_recursive()函数。

PHPFiddle: http://3v4l.org/5OCKI

PHPFiddle:http://3v4l.org/5OCKI

#2


1  

If you want the result to be a string then this should work:

如果你希望结果是一个字符串,那么这应该工作:

 foreach( $array_1 as $fruit => $num) {
        if(array_key_exists($fruit, $array_2)){ //check if key from array_1 exists in array_2
             $final_array[] = array($fruit => $array_1[$fruit].','.$array_2[$fruit]); //concatenate values from shared key
        }
    }

print_r($final_array) will return:

print_r($ final_array)将返回:

Array
(
    [0] => Array
        (
            [apple] => 1,2
        )

    [1] => Array
        (
            [banana] => 2,3
        )

    [2] => Array
        (
            [coconut] => 3,4
        )

)

#3


0  

<?php
    $array1 = array("apple" => 5, "banana" => 1);
    $array2 = array("banana" => 4, "coconut" => 6);
    print_r( array_merge_recursive( $array1, $array2 ) );
?>

Returns:

返回:

Array ( [apple] => 5 [banana] => Array ( [0] => 1 [1] => 4 ) [coconut] => 6 )

I only used two elements in each of the primary arrays to demonstrate the output prior to having any groups existing, i.e. non-array value.

我只在每个主阵列中使用了两个元素来演示在存在任何组之前的输出,即非数组值。

#1


6  

There is no such array in PHP. The thing you want can only be done by creating multidimentional arrays.

PHP中没有这样的数组。你想要的东西只能通过创建多维数组来完成。

$a1 = array( 'apple'=>1,'banana'=>2,'coconut'=> 3);

$a2 = array( 'apple'=>2,'banana'=>3,'coconut'=> 4);

echo "<pre>";
print_r(array_merge_recursive($a1,$a2));
echo "</pre>";

For this you can use the array_merge_recursive() function.

为此,您可以使用array_merge_recursive()函数。

PHPFiddle: http://3v4l.org/5OCKI

PHPFiddle:http://3v4l.org/5OCKI

#2


1  

If you want the result to be a string then this should work:

如果你希望结果是一个字符串,那么这应该工作:

 foreach( $array_1 as $fruit => $num) {
        if(array_key_exists($fruit, $array_2)){ //check if key from array_1 exists in array_2
             $final_array[] = array($fruit => $array_1[$fruit].','.$array_2[$fruit]); //concatenate values from shared key
        }
    }

print_r($final_array) will return:

print_r($ final_array)将返回:

Array
(
    [0] => Array
        (
            [apple] => 1,2
        )

    [1] => Array
        (
            [banana] => 2,3
        )

    [2] => Array
        (
            [coconut] => 3,4
        )

)

#3


0  

<?php
    $array1 = array("apple" => 5, "banana" => 1);
    $array2 = array("banana" => 4, "coconut" => 6);
    print_r( array_merge_recursive( $array1, $array2 ) );
?>

Returns:

返回:

Array ( [apple] => 5 [banana] => Array ( [0] => 1 [1] => 4 ) [coconut] => 6 )

I only used two elements in each of the primary arrays to demonstrate the output prior to having any groups existing, i.e. non-array value.

我只在每个主阵列中使用了两个元素来演示在存在任何组之前的输出,即非数组值。