久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php json常用方法有哪些

      php json常用方法:1、json_encode(),用于對(duì)JSON格式的字符串進(jìn)行解碼;2、json_encode(),用于對(duì)JSON格式的字符串進(jìn)行解碼;3、json_last_error(),用于返回最后發(fā)生的錯(cuò)誤。

      php json常用方法有哪些

      本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

      php json常用方法:

      1、json_encode()

      PHP json_encode() 用于對(duì)變量進(jìn)行 JSON 編碼,該函數(shù)如果執(zhí)行成功返回 JSON 數(shù)據(jù),否則返回 FALSE 。

      語法

      string json_encode ( $value [, $options = 0 ] )

      示例:

      <?php    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    echo json_encode($arr); ?>

      輸出結(jié)果:

      {"a":1,"b":2,"c":3,"d":4,"e":5}

      2、json_encode()

      json_decode() 函數(shù)用于對(duì) JSON 格式的字符串進(jìn)行解碼,并轉(zhuǎn)換為 PHP 變量。

      語法:

      mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

      參數(shù):

      • json_string: 待解碼的 JSON 字符串,必須是 UTF-8 編碼數(shù)據(jù)

      • assoc: 當(dāng)該參數(shù)為 TRUE 時(shí),將返回?cái)?shù)組,F(xiàn)ALSE 時(shí)返回對(duì)象。

      • depth: 整數(shù)類型的參數(shù),它指定遞歸深度

      • options: 二進(jìn)制掩碼,目前只支持 JSON_BIGINT_AS_STRING 。

      示例:

      <?php    $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';     var_dump(json_decode($json));    var_dump(json_decode($json, true)); ?>

      輸出結(jié)果:

      object(stdClass)#1 (5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5) }  array(5) {     ["a"] => int(1)     ["b"] => int(2)     ["c"] => int(3)     ["d"] => int(4)     ["e"] => int(5) }

      3、json_last_error()

      json_last_error — 返回最后發(fā)生的錯(cuò)誤

      語法:

      json_last_error()

      如果有,返回 JSON 編碼解碼時(shí)最后發(fā)生的錯(cuò)誤。會(huì)返回一個(gè)整型(integer),這個(gè)值會(huì)是以下的常量之一:

      JSON 錯(cuò)誤碼
      常量 含義 可用性
      JSON_ERROR_NONE 沒有錯(cuò)誤發(fā)生
      JSON_ERROR_DEPTH 到達(dá)了最大堆棧深度
      JSON_ERROR_STATE_MISMATCH 無效或異常的 JSON
      JSON_ERROR_CTRL_CHAR 控制字符錯(cuò)誤,可能是編碼不對(duì)
      JSON_ERROR_SYNTAX 語法錯(cuò)誤
      JSON_ERROR_UTF8 異常的 UTF-8 字符,也許是因?yàn)椴徽_的編碼。 PHP 5.3.3
      JSON_ERROR_RECURSION One or more recursive references in the value to be encoded PHP 5.5.0
      JSON_ERROR_INF_OR_NAN One or more NAN or INF values in the value to be encoded PHP 5.5.0
      JSON_ERROR_UNSUPPORTED_TYPE 指定的類型,值無法編碼。 PHP 5.5.0
      JSON_ERROR_INVALID_PROPERTY_NAME 指定的屬性名無法編碼。 PHP 7.0.0
      JSON_ERROR_UTF16 畸形的 UTF-16 字符,可能因?yàn)樽址幋a不正確。 PHP 7.0.0

      示例:

      <?php // 一個(gè)有效的 json 字符串 $json[] = '{"Organization": "PHP Documentation Team"}';  // 一個(gè)無效的 json 字符串會(huì)導(dǎo)致一個(gè)語法錯(cuò)誤,在這個(gè)例子里我們使用 ' 代替了 " 作為引號(hào) $json[] = "{'Organization': 'PHP Documentation Team'}";   foreach ($json as $string) {     echo 'Decoding: ' . $string;     json_decode($string);      switch (json_last_error()) {         case JSON_ERROR_NONE:             echo ' - No errors';         break;         case JSON_ERROR_DEPTH:             echo ' - Maximum stack depth exceeded';         break;         case JSON_ERROR_STATE_MISMATCH:             echo ' - Underflow or the modes mismatch';         break;         case JSON_ERROR_CTRL_CHAR:             echo ' - Unexpected control character found';         break;         case JSON_ERROR_SYNTAX:             echo ' - Syntax error, malformed JSON';         break;         case JSON_ERROR_UTF8:             echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';         break;         default:             echo ' - Unknown error';         break;     }      echo PHP_EOL; } ?>

      輸出結(jié)果:

      Decoding: {"Organization": "PHP Documentation Team"} - No errors Decoding: {'Organization': 'PHP Documentation Team'} - Syntax error, malformed JSON

      推薦學(xué)習(xí):《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)