2017-01-02 96 views
0

我試試這個數組與array_merge()不起作用。如何用數組值替換數組值[有兩個數組]

我有數組(數組一);

Array (
    [0] => mf_3 
    [1] => mf_2 
    [2] => mf_1 
    [3] => mf_7 
    [4] => mf_6 
    [5] => mf_4 
) 

並再次陣列(第二陣列),

Array (
    [0] => stdClass Object 
     (
      [id] => 1 
      [name] => review_smartphone_display 
      [label] => Layar 
      [post_type] => review_smartphone 
     ) 

    [1] => stdClass Object 
     (
      [id] => 2 
      [name] => review_smartphone_launch 
      [label] => Peluncuran 
      [post_type] => review_smartphone 
     ) 

    [2] => stdClass Object 
     (
      [id] => 3 
      [name] => review_smartphone_platform 
      [label] => Platform 
      [post_type] => review_smartphone 
     ) 

    [3] => stdClass Object 
     (
      [id] => 4 
      [name] => review_smartphone_camera 
      [label] => Kamera 
      [post_type] => review_smartphone 
     ) 

    [4] => stdClass Object 
     (
      [id] => 6 
      [name] => review_smartphone_design 
      [label] => Desain 
      [post_type] => review_smartphone 
     ) 

    [5] => stdClass Object 
     (
      [id] => 7 
      [name] => review_smartphone_battery 
      [label] => Baterai 
      [post_type] => review_smartphone 
     ) 
) 

現在,如何與值替換第二陣列[ID]從陣列的一個。

我想要這樣的結果;

Array (
     [0] => stdClass Object 
      (
       [id] => mf_3 
       [name] => review_smartphone_display 
       [label] => Layar 
       [post_type] => review_smartphone 
      ) 

     [1] => stdClass Object 
      (
       [id] => mf_2 
       [name] => review_smartphone_launch 
       [label] => Peluncuran 
       [post_type] => review_smartphone 
      ) 

     [2] => stdClass Object 
      (
       [id] => mf_1 
       [name] => review_smartphone_platform 
       [label] => Platform 
       [post_type] => review_smartphone 
      ) 

     [3] => stdClass Object 
      (
       [id] => mf_7 
       [name] => review_smartphone_camera 
       [label] => Kamera 
       [post_type] => review_smartphone 
      ) 

     [4] => stdClass Object 
      (
       [id] => mf_6 
       [name] => review_smartphone_design 
       [label] => Desain 
       [post_type] => review_smartphone 
      ) 

     [5] => stdClass Object 
      (
       [id] => mf_4 
       [name] => review_smartphone_battery 
       [label] => Baterai 
       [post_type] => review_smartphone 
      ) 
    ) 

請幫忙,謝謝。

+0

你有沒有試着寫一個簡單的foreach循環? – RiggsFolly

+1

你必須使用for循環。邏輯並不難,你必須使用函數。 –

+1

@AkhterAlaminFarhan你不必爲foreach和for循環之間的 – RiggsFolly

回答

0

製作使用的foreach像下面

foreach($first_array as $index => $farray) 
{ 
    $second_array[$index]->id = $farray; 
} 
+0

讓我知道它是否適用於你 –

+1

它可能如果'$ second_array [$ index] - > id = $ farray;' – RiggsFolly

+0

是@RiggsFolly,我發佈了答案匆忙,謝謝 –

0
foreach ($first_array as $key => $value) { 
    $second_array[$key]->id = $value; 
} 
+5

很高興爲你所做的事和原因添加一點點描述 – RiggsFolly