2011-04-12 4 views
1

다음 배열을 가지고 있는데, 헛소리로 가득 차 있습니다 (lorem ipsum을 사용하여 지루해 졌으므로 임의의 내용을 입력하기 시작했습니다 - 저는 ¬_¬을 테스트 중입니다).배열 끝으로 값 밀어 내기

이 배열을 가져 오기 위해 mysqli_fetch_array를 사용했습니다.

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

제가 제 배열 IDS는 매치에이 제 2 어레이를 푸시 할 :

는 [배열 1]

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 
       ) 

     ) 

) 

I 다른 배열 [배열 2]이있다.

그래서 첫 번째 배열이 될 것입니다 :

[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'] 
} 

답변

0

이 시도 .id 및 array1.thread_id는 동일합니다. 이 경우 array_merge를 사용할 수 없습니다. 두 번째 배열을 아래와 같이 변경하지 않는 한.

어레이 ( [$ thead_id] => 어레이 ([회신] => 2) .... ) 는 쉽게 array_merge를 사용할 수있다.

0

당신이 내가 당신의 질문이 정확 무엇입니까이라면 어디 배열 2 배열을 병합하려는 테이블

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

    } 

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

에 가입하여도이 작업을 수행 할 수 있음을 보인다