8

我正在尝试将以下两个数组合并为一个数组,共享相同的密钥:

第一个数组:

array(3) {
  [0]=>
   array(1) {
   ["Camera1"]=>
   string(14) "192.168.101.71"
}
[1]=>
array(1) {
  ["Camera2"]=>
  string(14) "192.168.101.72"
}
[2]=>
array(1) {
  ["Camera3"]=>
  string(14) "192.168.101.74"
}
}

第二个数组:

array(3) {
 [0]=>
  array(1) {
  ["Camera1"]=>
  string(2) "VT"
 }
 [1]=>
 array(1) {
   ["Camera2"]=>
   string(2) "UB"
 }
 [2]=>
 array(1) {
  ["Camera3"]=>
  string(2) "FX"
 }
}

如您所见,它们共享相同的密钥(Camera1、Camera2、Camera3 等)

这是我尝试过的:

 $Testvar = array_merge($NewArrayCam,$IpAddressArray);
 foreach ($Testvar AS $Newvals){
 $cam = array();
 foreach($Newvals AS $K => $V){
 $cam[] = array($K => $V);
 }
4

10 回答 10

11

理想情况下,我希望以一种array_merge_recursive可以简单地合并数组而不会大惊小怪的方式来格式化两个数组。

但是我确实想出了一个使用array_map.

$array1 = array(
    array("Camera1" => "192.168.101.71"),
    array("Camera2" => "192.168.101.72"),
    array("Camera3" => "192.168.101.74"),
);

$array2 = array(
    array("Camera1" => "VT"),
    array("Camera2" => "UB"),
    array("Camera3" => "FX")
);

$results = array();

array_map(function($a, $b) use (&$results) {

    $key = current(array_keys($a));
    $a[$key] = array('ip' => $a[$key]);

    // Obtain the key again as the second array may have a different key.
    $key = current(array_keys($b));
    $b[$key] = array('name' => $b[$key]);

    $results += array_merge_recursive($a, $b);

}, $array1, $array2);

var_dump($results);

输出是:

array (size=3)
  'Camera1' => 
    array (size=2)
      'ip' => string '192.168.101.71' (length=14)
      'name' => string 'VT' (length=2)
  'Camera2' => 
    array (size=2)
      'ip' => string '192.168.101.72' (length=14)
      'name' => string 'UB' (length=2)
  'Camera3' => 
    array (size=2)
      'ip' => string '192.168.101.74' (length=14)
      'name' => string 'FX' (length=2)
于 2013-02-12T23:30:27.000 回答
8

使用 array_merge_recursive :

将所有数字键转换为字符串,(make是关联数组)

$result = array_merge_recursive($ar1, $ar2);
print_r($result);

参考: http: //php.net/array_merge_recursive

于 2013-02-13T05:42:00.143 回答
8

尝试使用array_merge_recursive

于 2013-02-12T21:43:07.087 回答
6

对于您的嵌套级别就足够了:

$sumArray = array_map(function ($a1, $b1) { return $a1 + $b1; }, $array1, $array2);

对于更深的嵌套,它不起作用。

于 2017-04-05T17:10:06.990 回答
2
this would be one of the soluion:

function array_merge_custom($array1,$array2) {
    $mergeArray = [];
    $array1Keys = array_keys($array1);
    $array2Keys = array_keys($array2);
    $keys = array_merge($array1Keys,$array2Keys);

    foreach($keys as $key) {
        $mergeArray[$key] = array_merge_recursive(isset($array1[$key])?$array1[$key]:[],isset($array2[$key])?$array2[$key]:[]);
    }

    return $mergeArray;

}

$array1 = array(
    array("Camera1" => "192.168.101.71"),
    array("Camera2" => "192.168.101.72"),
    array("Camera3" => "192.168.101.74"),
);

$array2 = array(
    array("Camera1" => "VT"),
    array("Camera2" => "UB"),
    array("Camera3" => "FX")
);
echo '<pre>';
print_r(array_merge_custom($array1 , $array2));
于 2014-09-17T06:04:36.083 回答
2

如果两个数组具有相同数量的级别和键,则应该可以:

$array3 = array();

foreach ($array1 as $key1 => $value1) {
  // store IP
  $array3['Camera'.$key1]['IP'] = $value['Camera'.$key1]; 
  // store type of cam
  $array3['Camera'.$key1]['Type'] = $array2[$key]['Camera'.$key1]; 

}

最后 $array3 应该是这样的:

$array3 = array {

["Camera1"] => {['IP'] => "192.168.101.71", ['Type'] => "VT" }
["Camera2"] => {['IP'] => "192.168.101.72", ['Type'] => "UB" }
["Camera3"] => {['IP'] => "192.168.101.74", ['Type'] => "FX" }

}
于 2013-02-12T21:48:15.603 回答
0

像这样的东西应该工作:

$array1 = array(array("Camera1" => "192.168.101.71"), array("Camera2" => "192.168.101.72"), array("Camera3" => "192.168.101.74"));
$array2 = array(array("Camera1" => "VT"), array("Camera2" => "UB"), array("Camera3" => "FX"));
$results = array();

foreach($array1 as $key => $array){
  foreach($array as $camera => $value){
    $results[$camera]['ip'] = $value;
  }
}

foreach($array2 as $key => $array){
  foreach($array as $camera => $value){
    $results[$camera]['name'] = $value;
  }
}
print_r($results);
于 2013-02-12T22:03:19.317 回答
0

主要问题是数组。由于它们的结构方式,合并它们变得不必要地复杂。它们只是普通的关联数组(即array('Camera1' => 'VT'),合并它们将毫不费力。

我建议您弄清楚如何以一种更易于使用的方式格式化数据。

这是合并两个数组的一种快速而肮脏的方式。它从一个数组中取出一个“相机”,然后尝试在另一个数组中找到相应的“相机”。该函数仅使用$ips数组中的“相机”,并且仅使用匹配CameraN键。

$ips = array(
    array('Camera1' => '192.168.101.71'),
    array('Camera2' => '192.168.101.72'),
    array('Camera3' => '192.168.101.74'),
);
$names = array(
    array('Camera1' => 'VT'),
    array('Camera2' => 'UB'),
    array('Camera3' => 'FX'),
);
function combineCameras($ips, $names) {
    $output = array();
    while ($ip = array_shift($ips)) {
        $ident = key($ip);
        foreach ($names as $key => $name) {
            if (key($name) === $ident) {
                $output[$ident] = array(
                    'name' => array_shift($name),
                    'ip' => array_shift($ip),
                );
                unset($names[$key]);
            }
        }
    }
    return $output;
}
var_dump(combineCameras($ips, $names));
于 2013-02-12T22:32:11.030 回答
0

您可以将所有数字键转换为字符串并使用array_replace_recursive

将一个或多个数组的元素合并在一起,以便将一个数组的值附加到前一个数组的末尾。它返回结果数组。

例子

$arr1 = [
    'rate' => 100   
];

$arr2 = [
    'rate' => 100,
    'name' => 'Best Name In Town',
];

print_r(array_replace_recursive($arr1, $arr2));

输出

Array
(
    [rate] => 100
    [name] => Best Name In Town
)
于 2021-10-13T21:54:01.573 回答
0

这对我有用。我用相同的键加入了两个数组

$array1 = ArrayUtils::merge($array1, $array2);

如果您需要保留 NumericKey,请使用

$array1 = ArrayUtils::merge($array1, $array2, true);
于 2016-12-01T13:00:55.210 回答