2016-05-14 392 views
1

我有一些問題如何合併這個數組。可以幫助我嗎?PHP合併數組對象

第一陣列:

 
Array 
    (
     [22] => WP_Post Object 
      (
       [ID] => 22 
       [post_author] => 1 

      ) 

     [23] => WP_Post Object 
      (
       [ID] => 23 
       [post_author] => 1 

      ) 

    ) 

第二陣列:

 
Array 
    (
     [0] => stdClass Object 
      (
       [img_thumb] => small_duck.jpg 
       [img_full] => duck.jpg 
      ) 

     [1] => stdClass Object 
      (
       [img_thumb] => small_fish.jpg 
       [img_full] => fish.jpg 
      ) 

    ) 

應該輸出:

 
    Array 
     (
      [22] => WP_Post Object 
       (
        [ID] => 22 
        [post_author] => 1 
        [img_thumb] => small_duck.jpg 
        [img_full] => duck.jpg 

       ) 

      [23] => WP_Post Object 
       (
        [ID] => 23 
        [post_author] => 1 
        [img_thumb] => small_fish.jpg 
        [img_full] => fish.jpg 

       ) 

     ) 

陣列科Ÿ跟隨第一陣列,

+0

考慮使用PHP解決http://php.net/manual/en/function.array-merge.php – Fil

回答

1

這工作...

$first = array(
    22 => array(
     'ID' => 22, 
     'post_author' => 1 
    ), 
    23 => array(
     'ID' => 23, 
     'post_author' => 1 
    ) 
); 
$second = array(
    array(
     'img_thumb' => 'small_duck.jpg', 
     'img_full' => 'duck.jpg' 
    ), 
    array(
     'img_thumb' => 'small_fish.jpg', 
     'img_full' => 'fish.jpg' 
    ) 
); 

echo '<pre>'; 
var_dump($first); 
var_dump($second); 

$i=0; 
$should = array(); 
foreach ($first as $key => $arr) { 
    if(isset($second[$i])) 
     $arr = array_merge($arr,$second[$i]); 

    $should[$key] = $arr; 
    $i++; 
} 

var_dump($should); 
+0

的array_merge功能,爲感謝@Barry – Ofri