2014-04-01 56 views
0

我有一個像下面兩個數組:合併兩個單獨的數組在PHP

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 

所以我試圖array_merge這就像array_merge($results1["result1"],$results2["result2"]);但我正在逐漸如何做到這一點的第2個數組的結果呢?我得到的結果一樣:

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

你有一個多維數組,所以使用array_merge_recursive

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

例如:

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

給你

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

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

給你預期的

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

好吧,我解決它以另一種方式。我採取了result1 [「實體」]和結果2 [「實體」],然後我得到了我想要的值。 –