php if條件判斷數組返回[false]

來源:互聯網
上載者:User
{"code":200,"message":"success","data":[false]}

 $sql_notice=mysql_query(sql語句)    $arrData =array();    while ($arrData[] = mysql_fetch_array($sql_notice))    {       $arrData;    }    $sData = array_values($arrData);    if(。。。){    Response::json(200,'success',$sData);    }else{    Response::json(400,'error',"");    }

請注意 是[false] 不是false
因為前面應需要加了[]

這個是做的轉json返回資料的介面 while迴圈那裡若有最佳化的寫法也請指教
現在就是因為這個 若不符合返回的會是[false] 試著用和false一樣的方法去判斷結果都不行
求教這裡要怎麼寫


回複討論(解決方案)

1、你需要得到什嗎?哪裡出了問題?
2、Response::json 方法是怎麼寫的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 將會使 $arrData 數組的最後一個元素為 false

1、你需要得到什嗎?哪裡出了問題?
2、Response::json 方法是怎麼寫的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 將會使 $arrData 數組的最後一個元素為 false


做一個APP介面返回json資料 資料庫讀出多條資料 也就是多個數組 遍曆 轉為json 返回給app端
現在請求驗證通過的話資料都可以讀出並以json呈現 但當請求沒有通過驗證致使讀取的資料為空白時,返回仍是200 成功
驗證條件就是讀取資料庫的條件 不符合就讀不到 然後判斷數組為空白就應該400 但是由於這個[false]不被視為空白,所以結果會返回
200 成功 [false] 這樣就不行了 應該這種情況下是400失敗的

1、你需要得到什嗎?哪裡出了問題?
2、Response::json 方法是怎麼寫的
3、while ($arrData[] = mysql_fetch_array($sql_notice)) 將會使 $arrData 數組的最後一個元素為 false



class Response {const JSON = "json";/*** * @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $data 資料* @param string $type 資料類型* return string*/public static function show($code, $message = '', $data = array(), $type = self::JSON) {if(!is_numeric($code)) {return '';}$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;$result = array('code' => $code,'message' => $message,'data' => $data,);if($type == 'json') {self::json($code, $message, $data);exit;}else{// TODO}}/*** 按json方式輸出通訊資料* @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $data 資料* return string*/public static function json($code, $message = '', $data = array()) {if(!is_numeric($code)) {return '';}$result = array('code' => $code,'message' => $message,'data' => $data);echo json_encode($result);exit;}}


如果是while那句的寫法有問題導致的話 求教怎麼改

    $arrData =array();    while ($$row = mysql_fetch_array($sql_notice))    {       $arrData[] = $row;    }    if($arrData) {      Response::json(200,'success',$arrData);    } .....

我不關心你的目的是什麼,只關心你需要什麼樣的格式

    $arrData =array();    while ($$row = mysql_fetch_array($sql_notice))    {       $arrData[] = $row;    }    if($arrData) {      Response::json(200,'success',$arrData);    } .....

我不關心你的目的是什麼,只關心你需要什麼樣的格式



按我當前的模式 驗證條件即是查詢條件的話 判斷自然是是否有資料了
我現在需要的是有就用json格式輸出 返回200 成功 資料 如果沒有 就返回 400 失敗 ""

    $arrData =array();    while ($$row = mysql_fetch_array($sql_notice))    {       $arrData[] = $row;    }    if($arrData) {      Response::json(200,'success',$arrData);    } .....

我不關心你的目的是什麼,只關心你需要什麼樣的格式



受教了 感謝版主大大
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.