PHP는

2014-05-09 3 views
-3

내가 JSON 배열 출력을 가지고 중복 된 JSON 배열을 받고 있지만, 문제가 무엇 0 번째 인덱스에 중복 json_array ... 나도 몰라 을 볼 수 아래는 중복 값 을 포함 필요 제안 및 왜...? 미리 감사드립니다PHP는

내 출력 :

[{"gallery_url":"fdkgvdjvb.img"}][{"gallery_url":"fdkgvdjvb.img"},{"gallery_url":"gdfgh.mp4"}] 

필수 출력 :

[{"gallery_url":"fdkgvdjvb.img"},{"gallery_url":"gdfgh.mp4"}] 

내 코드 :

<?php 
$json = array(); 
$gallery=$_GET['gallery']; 



     $con=mysqli_connect("localhost","allluser","password4"); 
    if($con) 
    { 
    //echo "connected"; 
    } 
    $db_select = mysqli_select_db($con,'jumeirah_db'); 
    if($db_select) 
    { 
    //echo "db selected"; 
    } 
    $user_type=$_GET['user_type']; 
    $image=mysqli_query($con,"Select gallery_url from ju_gallery where user_type='$user_type'"); 
if ($image) { 


while($r = mysqli_fetch_assoc($image)) { 
    $json[] = $r; 
    print json_encode($json); 
} 
} 
mysqli_close($con); 

?> 
+0

'while' 루프 내부에서'print json_encode ($ json);'문을 꺼내서 꺼내십시오. –

+0

예, 작동합니다 ,,, 고마워요.하지만, 왜 이런 일이 생깁니 까. – arun

+0

간단합니다. @arun. 각 행에 대해 JSON으로 인코딩 된 값을 인쇄하고 있습니다. ** 전체 배열 **의 JSON 표현 만 있으면됩니다. –

답변

0

json_encode 문을 가져옵니다.

<?php 
$json = array(); 
$gallery=$_GET['gallery']; 

     $con=mysqli_connect("localhost","allluser","password4"); 
    if($con) 
    { 
    //echo "connected"; 
    } 
    $db_select = mysqli_select_db($con,'jumeirah_db'); 
    if($db_select) 
    { 
    //echo "db selected"; 
    } 
    $user_type=$_GET['user_type']; 
    $image=mysqli_query($con,"Select gallery_url from ju_gallery where user_type='$user_type'"); 
if ($image) { 


while($r = mysqli_fetch_assoc($image)) { 
    $json[] = $r; 
} 
print json_encode($json); 
} 
mysqli_close($con); 

?> 
1

은 예상 된 결과를 인쇄해야

while($r = mysqli_fetch_assoc($image)) { 
    $json[] = $r; 
} 

print json_encode($json); 

을 시도합니다.

데이터베이스는 실제로 while 루프에서 정확히 두 번의 반복을 유발하는 두 개의 결과를 반환합니다.

첫 번째 반복은 $ json 배열에 $ r을 추가합니다. 이제 배열에는 하나의 요소가 있고 그것을 인쇄합니다.

두 번째 반복은 $ json 배열에 또 다른 $ r을 추가합니다.이 배열에는 이제 두 개의 요소가 있습니다. 두 번째 print이 예상 결과를 출력합니다.

"복제"는 while 루프 내부의 인쇄로 인해 발생합니다.