2014-02-28 1 views
0

내 결과를 json으로 인코딩 했으므로 병합하려고합니다. 나는 각 문이인코딩 된 json을 다른 json으로 푸시

[ 
    { 
     "post_id": 1, 
     "content": "test image feature", 
     "date": "0000-00-00", 
     "category_id": 1, 
     "lp_title": "", 
     "lp_image": "", 
     "lp_canonicalUrl": "", 
     "lp_url": "", 
     "lp_desc": "", 
     "lp_iframe": "", 
     "lp_iframe_id": "" 
     "img_src" [{img_src:1.jpg},{img_src:2.jpg}] 
    } 
] 

http://pastebin.com/7jKp9BUn

결과를 달성 할

[ 
    { 
     "img_src": "1.jpg" 
    }, 
    { 
     "img_src": "2.jpg" 
    } 
] 

당신이 하나를 추가, 배열로 JSON을 디코딩 할 수있는 하나

[ 
    { 
     "post_id": 1, 
     "content": "test image feature", 
     "date": "0000-00-00", 
     "category_id": 1, 
     "lp_title": "", 
     "lp_image": "", 
     "lp_canonicalUrl": "", 
     "lp_url": "", 
     "lp_desc": "", 
     "lp_iframe": "", 
     "lp_iframe_id": "" 
    } 
] 

답변

3

다음 다른 하나는 다음 다시 인코딩 :

$s1 = '[ 
    { 
     "img_src": "1.jpg" 
    }, 
    { 
     "img_src": "2.jpg" 
    } 
]'; 
$s2 = '[ 
    { 
     "post_id": 1, 
     "content": "test image feature", 
     "date": "0000-00-00", 
     "category_id": 1, 
     "lp_title": "", 
     "lp_image": "", 
     "lp_canonicalUrl": "", 
     "lp_url": "", 
     "lp_desc": "", 
     "lp_iframe": "", 
     "lp_iframe_id": "" 
    } 
]'; 

//decode each 
$arr1 = json_decode($s1, true); 
$arr2 = json_decode($s2, true); 
$arr2[0]['img_src'] = $arr1; 
//re-encode as a JSON string and show output 
$sf = json_encode($arr2); 
echo $sf; 

이렇게하면 더 큰 세트의 하위 배열로 "img_src"...와 함께 설명 된 결과를 얻을 수 있습니다.

+0

확인이 이미지 : 가 http://i.imgur.com/rJKW9Mp.png –

+0

@AlanYong - 당신이 $ 주위 대신 다른 방법의 $의 arr1''의 아이를 arr2''만든 것 같습니다 . '$ arr1'은 작은 img_src를 가지고 있습니다. '$ arr2'는 나머지를 가지고 있습니다. 설명 된 경우'$ arr1'을'$ arr2'의 자식으로 추가해야합니다. – ChicagoRedSox

+0

그게 당신에게 ** 개체 **를 줄 것입니다. 'json_decode'의 두 번째 param은 배열을 반환하기 위해'true'이어야합니다. –

0

PHP 코드를 보면 한 번에 이렇게 할 수 있습니다.

$stmt = $db->prepare(" 
SELECT post_id,content,date,category_id,lp_title,lp_image,lp_canonicalUrl,lp_url,lp_desc,lp_iframe,lp_iframe_id 
    FROM post_items inner JOIN user ON post_items.user_id = user.user_id 
    WHERE post_items.user_id = ? order by post_items.post_id desc LIMIT ?,?"); 

$stmt->bind_param('iii', $userid, $itemStart, $itemEnd); 

$stmt2 = $db->prepare("SELECT photo_upload.img_src FROM photo_upload inner JOIN post_items ON post_items.photo_set_id = photo_upload.photo_set_id 
    WHERE post_items.photo_set_id = photo_upload.photo_set_id "); 

//store the images in a separate array 
$imgArray = array(); 
if ($stmt2->execute()) { 
    $photo_items = $stmt2->get_result(); 

    while ($obj2 = $photo_items->fetch_object()) { 
     $imgArray[] = $obj2; 
    }  
} 

if($stmt->execute()){ 

    $post_items = $stmt->get_result(); 

    if(mysqli_num_rows($post_items) > 0){ 
     while ($obj = $post_items->fetch_object()) { 

      $jsonData[] = $obj; 
        // check if there are any elements in the array 
      if(count($imgArray) > 0){ 
       // set the img_src index of the jsonData array with the elements of the other array 
       $jsonData['img_src'] = $imgArray; 
      } 
     } 




     $i = json_encode($jsonData); 

     echo $i; 

    } 
} 
+0

이 이미지를 확인하십시오 : http://i.imgur.com/4D1JxIv.png –

+0

네가 맞아. 나는 해답을 편집했으나 이것은'jsonData'의 모든 인덱스에 대해'img_src'에 대해 같은 값을 추가 할 것입니다. – anurupr

관련 문제