2011-04-12 228 views
1

我有以下數組,它充滿了亂碼(我厭倦了使用lorem ipsum,所以我開始輸入隨機的東西 - 我正在測試¬_¬)。我使用mysqli_fetch_array獲取這個數組。將數值推到數組的末尾

[ARRAY1]

Array 
(
    [0] => Array 
     (

      [title] => this is a new thread, and it is great and it should work 

      [thread_id] => 27 

      [content] => <p>hello, how are you? and what are you doing y'all and this should work</p> 

      [username] => umar 

      [author_id] => 12 

      [tags] => Array 
       (
        [0] => lorem 
       ) 

     ) 

    [1] => Array 
     (

      [title] => this is my second thread and it should work fine, just fine 

      [thread_id] => 28 

      [content] => <p>this is is good, that I think it should have a thread of its own, don't you think?</p> 

      [username] => umarrazzaq 

      [author_id] => 12 

      [tags] => Array 
       (
        [0] => thread 
        [1] => less 
       ) 

     ) 

) 

我有另一個數組[數組2]:

Array ([0] => Array ( [replies] => 2 [id] => 27) 
[1] => Array ( [replies] => 1 [id] => 28)) 

欲這個第二陣列推到第一陣列,其中,所述ID匹配。

例如

所以第一個數組將變成:

[0] => Array 
      (

       [title] => this is a new thread, and it is great and it should work 

       [thread_id] => 27 

       [content] => <p>hello, how are you? and what are you doing y'all and this should work</p> 

       [username] => umar 

       [author_id] => 12 

       [tags] => Array 
        (
         [0] => lorem 
        ) 

       **[replies] => 2** 

      ) 

我試着通過引用傳遞,並在foreach循環使用array_push,但它只做這一個。

回答

0

試試這個:

foreach($array1 as $key=>&$arr){ 
    $arr['replies'] = $array2[$key]['replies'] 
} 
0

看來,你可以,如果我得到你的問題正確的,那麼要合併數組,其中數組2做到這一點也與你的桌子上

foreach($array1 as $key => $value) 
    { 
    $array1[$value['thread_id']] = $value; 

    } 

    foreach($array2 as $value) 
    { 
     array_push($array1[$value['id']], $value); 
    } 
0

一個JOIN .id和array1.thread_id是相同的。在這種情況下,你不能使用array_merge。除非你像下面這樣改變你的第二個數組。

陣列( [$ thead_id] =>數組([回覆] => 2) .... ) 那麼可以使用array_merge容易。