2016-07-29 117 views
-1

我有一個php腳本,在我的android應用程序的json中給出響應。在json中添加響應代碼

的JSON:

{"0":"1","id":"1","1":"mahmudul hasan","name":"mahmudul hasan","2":"364","roll":"364","3":"sohel","qrcode":"sohel"} 

,但我也想與resonse象下面這樣的狀態:

{ 
    "0": "1", 
    "1": "mahmudul hasan", 
    "2": "364", 
    "3": "sohel", 
    "id": "1", 
    "name": "mahmudul hasan", 
    "roll": "364", 
    "qrcode": "sohel", 
    **"success": true** 
} 

我的JSON響應PHP男女同校:

foreach ($data as $item) 
    { 
     if($item!=NULL) 
     { 
      echo json_encode($item); 
     } 
     else 
     { 
      echo json_encode($item);    
     } 
    } 

應該如何我添加響應狀態?

回答

1

嘗試這樣的事情

foreach($data as $item) { 

    $item['status'] = false; 

    if (isset($item) && is_array($item)) { 
     $item['status'] = true; 
    } 

    echo json_encode($item); 
} 
從這個

除了你爲什麼不能嘗試: -

foreach ($data as $key => $item) { 
    if (is_array($item) && count($item) > 0) { 
     $data[$key]['status'] = true; 
    } else { 
     //unset($data[$key]) 
     $data[$key]['status'] = false; 
    } 
} 
echo json_encode($data); 
+0

讓我查一下。這是真的嗎? –

+0

@ sohel14_cse_ju編輯回答 –

+0

我可以返回空JSON沒有數據匹配嗎? –