2014-04-23 4 views
0

PHP로 새롭게 변경되었습니다. JSON 형식을 지정하려고합니다.PHP 웹 서비스 형식의 json 형식

$query = mysql_query("SELECT * 
FROM `micards` m 
JOIN user u ON m.`mobile` = u.phone"); 

while ($row = mysql_fetch_assoc($query)) { 
    $response["success"] = 1; 
    $name = array('name'=>$row["name"],'email'=>$row["email"],'mobile'=>$row["mobile"]); 
    $phone[] = array('phone'=>$row["phone"],array('card'=>$name)); 
    $response["contacts"] = $phone; 
} 
echo json_encode($response); 

내가이 출력 받고 있어요 :

{ 
    "success": 1, 
    "contacts": [{ 
     "phone": "919898989898", 
     "0": { 
     "card": { 
      "name": "abcd", 
      "email": "[email protected]", 
      "mobile": "919898989898" 
     } 
     } 
    }, { 
     "phone": "919898989898", 
     "0": { 
     "card": { 
      "name": "abcd", 
      "email": "[email protected]", 
      "mobile": "919898989898" 
     } 
     } 
    }, { 
     "phone": "8686868686", 
     "0": { 
     "card": { 
      "name": "pan", 
      "email": "[email protected]", 
      "mobile": "8686868686" 
     } 
     } 
    }, { 
     "phone": "8686868686", 
     "0": { 
     "card": { 
      "name": "monk", 
      "email": "[email protected]", 
      "mobile": "8686868686" 
     } 
     } 
    }] 
} 

을하지만 난이 원하는 : 여기에 관련 코드는

{ 
    "success": 1, 
    "contacts": [{ 
     "phone": "919898989898", 
     { 
      "card": { 
       "name": "abcd", 
       "email": "[email protected]", 
       "mobile": "919898989898" 
      } 
     }, 
     { 
      "card": { 
       "name": "abcd", 
       "email": "[email protected]", 
       "mobile": "919898989898" 
      } 
     } 
    }], 
    [{ 
     "phone": "8686868686", 
     { 
      "card": { 
       "name": "panky", 
       "email": "[email protected]", 
       "mobile": "8686868686" 
      } 
     }, 
     { 
      "card": { 
       "name": "panky", 
       "email": "[email protected]", 
       "mobile": "8686868686" 
      } 
     } 
    }] 
} 

나는 위의 출력을 얻기 위해 무엇을 할 수 있는가?

답변

0

바로이 부분 변경 :

while ($row = mysql_fetch_assoc($query)) { 
    $response["success"] = 1; 
    $name = array('name'=>$row["name"],'email'=>$row["email"],'mobile'=>$row["mobile"]); 
    $response["contacts"][] = array('phone'=>$row["phone"],array('card'=>$name)); 
} 
0

그냥 복사 및 붙여 넣기

$phone[] = array('phone'=>$row["phone"],array('card'=>$name)) to 
$phone[] = array('phone'=>$row["phone"],'card'=>$name) 
; 
0

변경하십시오이 코드를

$phone[] = array('phone'=>$row["phone"],array('card'=>$name));  
$response["contacts"] = $phone; 

$response["contacts"][] = array('phone'=>$row["phone"],array('card'=>$name)); 
+0

여러분 모두를위한 감사합니다. 그러나 위의 솔루션을 사용하지 않습니다 – user3395570

0

제 생각 엔 연관 배열에 문제가 있고 그것을 포장하는 방법이 있습니다. 다음과 같이 응답 배열을 구성 할 수 있습니다.

$response = [ 
    'success'=>1, 
    'contacts'=>[ 
     'phone'=>$row['phone'], 
     'card'=>[ 
      'name'=>$row['name'], 
      'email'=>$row['email'], 
      'mobile'=>$row['mobile'] 
     ] 
    ] 
]; 

제대로 작동하는지 확인하십시오.

+0

PHP입니까? 아니 . – Jerry

+0

예, PHP가 매우 느린 응답으로 죄송합니다 ... –