2016-07-10 4 views
-4

배열이 3 개 있습니다. 이러한 상호 관련. 나는이 문장들을 결합하고 싶다.php, 3 배열을 하나의 배열 병합 대체

<?php 

$post = array( 
    array (
     'id'  => 1, 
     'title' => 'Title 1', 
     'content' => 'Content 1' 
    ), 
    array (
     'id'  => 2, 
     'title' => 'Title 2', 
     'content' => 'Content 2' 
    ), 
    array (
     'id'  => 3, 
     'title' => 'Title 3', 
     'content' => 'Content 3' 
    ), 
); 


$user = array( 
    array (
     'id' => 1, 
     'name' => 'Mark' 
    ), 
    array (
     'id' => 2, 
     'name' => 'Selena' 
    ) 
); 

$post_user = array( 
    array (
     'id'  => 1, 
     'post_id' => 1, 
     'user_id' => 1 
    ), 
    array (
     'id'  => 2, 
     'post_id' => 2, 
     'user_id' => 1 
    ), 
    array (
     'id'  => 3, 
     'post_id' => 3, 
     'user_id' => 2 
    ), 
); 


$merge = array(); 
foreach($posts_user as $data){  
    foreach ($posts as $post){ 
     if($data['post_id'] == $post['id']){ 
      foreach ($users as $user){ 
       if($data['user_id'] == $user['id']){ 
       $post['user'] = $user; 
       $merge['post'][] = $post; 
       } 
      } 
     } 
    }  
} 

print_r($merge); 

다음과 같이되고 싶습니다. 내가 어떻게 해? 결과가 필요합니다.

$merge = array( 
    array (
     'id'  => 1, 
     'title' => 'Title 1', 
     'content' => 'Content 1', 
     'user' => array (
         'id' => '1', 
         'name' => 'Mark' 
        ), 

    ), 
    array (
     'id'  => 2, 
     'title' => 'Title 2', 
     'content' => 'Content 2', 
     'user' => array (
         'id' => '1', 
         'name' => 'Mark' 
        ), 
    ), 
    array (
     'id'  => 3, 
     'title' => 'Title 3', 
     'content' => 'Content 3', 
     'user' => array (
         'id' => '2', 
         'name' => 'Selena' 
        ), 
    ), 
); 

다른 대안이 있습니까? 예를 들어; 어떻게해야합니까?

array_walk_recursive(), ArrayIterator(), RecursiveArrayIterator()

내가하려고
+1

는 소리. – chris85

+0

단지 PHP에서이 작업을 수행하는 대신 적절한 sql 문을 사용합니다. chris가 말한 것처럼 – Ghost

+0

SQL을 사용하여 배열을 만듭니다. – Scaffold

답변

0

. 세 개의 쿼리를 사용하는 것처럼 조인 일해야 할 때

<?php  
$post = array( 
    array (
     'id'  => 1, 
     'title' => 'Title 1', 
     'content' => 'Content 1' 
    ), 
    array (
     'id'  => 2, 
     'title' => 'Title 2', 
     'content' => 'Content 2' 
    ), 
    array (
     'id'  => 3, 
     'title' => 'Title 3', 
     'content' => 'Content 3' 
    ), 
); 


$user = array( 
    array (
     'id' => 1, 
     'name' => 'Mark' 
    ), 
    array (
     'id' => 2, 
     'name' => 'Selena' 
    ) 
); 

$post_user = array( 
    array (
     'id'  => 1, 
     'post_id' => 1, 
     'user_id' => 1 
    ), 
    array (
     'id'  => 2, 
     'post_id' => 2, 
     'user_id' => 1 
    ), 
    array (
     'id'  => 3, 
     'post_id' => 3, 
     'user_id' => 2 
    ), 
); 

$newuser = array(); 
foreach($post_user as $data){ 
    foreach ($user as $users){ 
     if($data['user_id'] == $users['id']){ 
     $newuser[] = $users; 
     } 
    } 
} 



$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC); 
$mi->attachIterator(new ArrayIterator($post),1); 
$mi->attachIterator(new ArrayIterator($newuser),'user'); 

$newArray = array(); 
foreach($mi as $details) { 
    $newArray[] = $details; 
} 
print_r($newArray); 

이 결과

Array 
(
    [0] => Array 
     (
      [1] => Array 
       (
        [id] => 1 
        [title] => Title 1 
        [content] => Content 1 
       ) 

      [user] => Array 
       (
        [id] => 1 
        [name] => Mark 
       ) 

     ) 

    [1] => Array 
     (
      [1] => Array 
       (
        [id] => 2 
        [title] => Title 2 
        [content] => Content 2 
       ) 

      [user] => Array 
       (
        [id] => 1 
        [name] => Mark 
       ) 

     ) 

    [2] => Array 
     (
      [1] => Array 
       (
        [id] => 3 
        [title] => Title 3 
        [content] => Content 3 
       ) 

      [user] => Array 
       (
        [id] => 2 
        [name] => Selena 
       ) 

     ) 

)