PHP数组在同一个键上合并两个数组

时间:2022-01-09 12:21:55

I am trying to merge the following two arrays into one array, sharing the same key:

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

First Array:

第一个数组:

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

Second Array:

第二个数组:

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

As you can see, they share the same key (Camera1, Camera2, Camera3, etc..)

如你所见,它们共享同一个密钥(Camera1、Camera2、Camera3等)。

Here is what I have tried:

以下是我尝试过的:

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

9 个解决方案

#1


9  

Ideally I would look to format the two arrays in such a way that array_merge_recursive would simply merge the arrays without too much fuss.

理想情况下,我希望以array_merge_recursive这样的方式对这两个数组进行格式化,这样就可以简单地合并数组,而不会引起太大的麻烦。

However I did come up with a solution that used array_map.

然而,我确实提出了一个使用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);

The output is:

的输出是:

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)

#2


8  

Try to use array_merge_recursive.

尝试使用array_merge_recursive。

#3


6  

Use array_merge_recursive :

使用array_merge_recursive:

Convert all numeric key to strings, (make is associative array)

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

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

Ref : http://php.net/array_merge_recursive

裁判:http://php.net/array_merge_recursive

#4


5  

For your nesting level will be enough this:

对于您的嵌套级别将足够:

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

$sumArray = array_map(函数($a1, $b1){返回$a1 + $b1;},array1美元,美元array2);

For deeper nesting it wont work.

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

#5


2  

If both arrays have the same numbers of levels and keys this should work:

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

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

}

At the end $array3 should be something like:

最后,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" }

}

#6


1  

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

#7


0  

The main problem are the arrays. Because of the way they are structured it becomes unnecessarily complicated to merge them. It they simply were normal associative arrays (i.e. array('Camera1' => 'VT') then it would be effortless to merge them.

主要的问题是数组。由于它们的结构,合并它们变得不必要的复杂。如果它们只是普通的联合数组(即数组('Camera1' => 'VT')),那么合并它们就很容易了。

I would suggest that you figure out how to format the data in such a way as to make it easier to work with.

我建议您考虑如何以这样一种方式格式化数据,以使其更容易使用。

This is a quick and dirty way of merging the two arrays. It takes one "camera" from one array, and then tries to find the corresponding "camera" in the other array. The function only uses the "cameras" in the $ips array, and only uses matching CameraN keys.

这是一种快速而肮脏的合并两个数组的方法。它从一个数组中取出一个“camera”,然后尝试在另一个数组中找到对应的“camera”。该函数只使用$ips数组中的“camera”,并且只使用匹配的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));

#8


0  

Something like this should work:

像这样的东西应该可以:

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

#9


0  

This worked for me. I joined two arrays with the same keys

这为我工作。我用相同的键连接了两个数组

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

If you need preserve NumericKey, use

如果你需要保存数字键,请使用

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

#1


9  

Ideally I would look to format the two arrays in such a way that array_merge_recursive would simply merge the arrays without too much fuss.

理想情况下,我希望以array_merge_recursive这样的方式对这两个数组进行格式化,这样就可以简单地合并数组,而不会引起太大的麻烦。

However I did come up with a solution that used array_map.

然而,我确实提出了一个使用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);

The output is:

的输出是:

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)

#2


8  

Try to use array_merge_recursive.

尝试使用array_merge_recursive。

#3


6  

Use array_merge_recursive :

使用array_merge_recursive:

Convert all numeric key to strings, (make is associative array)

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

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

Ref : http://php.net/array_merge_recursive

裁判:http://php.net/array_merge_recursive

#4


5  

For your nesting level will be enough this:

对于您的嵌套级别将足够:

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

$sumArray = array_map(函数($a1, $b1){返回$a1 + $b1;},array1美元,美元array2);

For deeper nesting it wont work.

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

#5


2  

If both arrays have the same numbers of levels and keys this should work:

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

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

}

At the end $array3 should be something like:

最后,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" }

}

#6


1  

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

#7


0  

The main problem are the arrays. Because of the way they are structured it becomes unnecessarily complicated to merge them. It they simply were normal associative arrays (i.e. array('Camera1' => 'VT') then it would be effortless to merge them.

主要的问题是数组。由于它们的结构,合并它们变得不必要的复杂。如果它们只是普通的联合数组(即数组('Camera1' => 'VT')),那么合并它们就很容易了。

I would suggest that you figure out how to format the data in such a way as to make it easier to work with.

我建议您考虑如何以这样一种方式格式化数据,以使其更容易使用。

This is a quick and dirty way of merging the two arrays. It takes one "camera" from one array, and then tries to find the corresponding "camera" in the other array. The function only uses the "cameras" in the $ips array, and only uses matching CameraN keys.

这是一种快速而肮脏的合并两个数组的方法。它从一个数组中取出一个“camera”,然后尝试在另一个数组中找到对应的“camera”。该函数只使用$ips数组中的“camera”,并且只使用匹配的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));

#8


0  

Something like this should work:

像这样的东西应该可以:

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

#9


0  

This worked for me. I joined two arrays with the same keys

这为我工作。我用相同的键连接了两个数组

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

If you need preserve NumericKey, use

如果你需要保存数字键,请使用

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