在php中合并两个单独的数组

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

I have two arrays like below:

我有两个数组如下:

array
  'result1' => 
    array
      'entities' => 
        array
          0 => 
            object(ElggUser)[979]
              ...
      'count' => int 1
array
  'result2' => 
    array
      'entities' => 
        array
          0 => 
            object(ElggUser)[983]
              ...
          1 => 
            object(ElggUser)[986]
              ...
          2 => 
            object(ElggUser)[989]
              ...
          3 => 
            object(ElggUser)[992]
              ...
          4 => 
            object(ElggUser)[995]
              ...
          5 => 
            object(ElggUser)[998]
              ...
          6 => 
            object(ElggUser)[1001]
              ...
          7 => 
            object(ElggUser)[1004]
              ...
          8 => 
            object(ElggUser)[1007]
              ...
          9 => 
            object(ElggUser)[1010]
              ...
      'count' => int 1453

So I have tried array_merge for this like array_merge($results1["result1"],$results2["result2"]); but I am getting the 2nd array as result how do it? I am getting the result like:

所以我为array_merge尝试了array_merge($ results1 [“result1”],$ results2 [“result2”]);但是我得到的第二个数组是怎么做到的?我得到的结果如下:

array
      'entities' => 
        array
          0 => 
            object(ElggUser)[983]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          1 => 
            object(ElggUser)[986]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          2 => 
            object(ElggUser)[989]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          3 => 
            object(ElggUser)[992]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          4 => 
            object(ElggUser)[995]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          5 => 
            object(ElggUser)[998]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          6 => 
            object(ElggUser)[1001]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          7 => 
            object(ElggUser)[1004]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          8 => 
            object(ElggUser)[1007]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
          9 => 
            object(ElggUser)[1010]
              protected 'attributes' => 
                array
                  ...
              protected 'url_override' => null
              protected 'icon_override' => null
              protected 'temp_metadata' => 
                array
                  ...
              protected 'temp_annotations' => 
                array
                  ...
              protected 'volatile' => 
                array
                  ...
              private 'valid' (ElggEntity) => boolean false
      'count' => int 1453

1 个解决方案

#1


1  

You have a multidimensional array, so use array_merge_recursive:

你有一个多维数组,所以使用array_merge_recursive:

$result = array_merge_recursive($results1["result1"], $results2["result2"]);

for example:

例如:

$result1 = array("result1" => array("entities" => array(1, 2, 3)));
$result2 = array("result2" => array("entities" => array(4, 5, 6)));

var_dump(array_merge($result1["result1"], $result2["result2"]));

gives you

给你

array(1) {
  'entities' =>
  array(3) {
    [0] =>
    int(4)
    [1] =>
    int(5)
    [2] =>
    int(6)
  }
}

while

var_dump(array_merge_recursive($result1["result1"], $result2["result2"]));

gives you the expected

给你预期的

array(1) {
  'entities' =>
  array(6) {
    [0] =>
    int(1)
    [1] =>
    int(2)
    [2] =>
    int(3)
    [3] =>
    int(4)
    [4] =>
    int(5)
    [5] =>
    int(6)
  }
}

#1


1  

You have a multidimensional array, so use array_merge_recursive:

你有一个多维数组,所以使用array_merge_recursive:

$result = array_merge_recursive($results1["result1"], $results2["result2"]);

for example:

例如:

$result1 = array("result1" => array("entities" => array(1, 2, 3)));
$result2 = array("result2" => array("entities" => array(4, 5, 6)));

var_dump(array_merge($result1["result1"], $result2["result2"]));

gives you

给你

array(1) {
  'entities' =>
  array(3) {
    [0] =>
    int(4)
    [1] =>
    int(5)
    [2] =>
    int(6)
  }
}

while

var_dump(array_merge_recursive($result1["result1"], $result2["result2"]));

gives you the expected

给你预期的

array(1) {
  'entities' =>
  array(6) {
    [0] =>
    int(1)
    [1] =>
    int(2)
    [2] =>
    int(3)
    [3] =>
    int(4)
    [4] =>
    int(5)
    [5] =>
    int(6)
  }
}