将数值推到数组的末尾

将数值推到数组的末尾

问题描述:

我有以下数组,它充满了乱码(我厌倦了使用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,但它只做这一个。

试试这个:

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

看来,你可以,如果我得到你的问题正确的,那么要合并数组,其中数组2做到这一点也与你的桌子上

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

    } 

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

一个JOIN .id和array1.thread_id是相同的。在这种情况下,你不能使用array_merge。除非你像下面这样改变你的第二个数组。

阵列( [$ thead_id] =>数组([回复] => 2) .... ) 那么可以使用array_merge容易。