***php解析JSON 2 D配列文字列(json_decode関数2番目のパラメータTrueとFalseの違い)

6978 ワード

           :[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id": 2, "msg_status": "HAS_READ" }]           
$json_data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
JSON PHP ,
: true PHP , true PHP


   /**

     *        ID            

     */

    public function update_status_batch()

    {

        //       

/*        $data = array(

            array(

                'msg_id' => 1 ,

                'msg_status' => 'HAS_READ'

            ),

            array(

                'msg_id' => 2 ,

                'msg_status' => 'HAS_READ'

            )

        );*/



        //      JSON  ,         TRUE   ,       ;        JSON   ,            PHP  

        $json_data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);

/*        //    

        //$item_num = count($json_data);

        //      

        $array = array();

        foreach($json_data as $item){

            $array_unit = array(

                'msg_id' => $item->msg_id,

                'msg_status' => $item->msg_status

            );

            //         

            array_push($array,$array_unit);

        }*/



        //

        $result = $this->m_user_msg->update_batch($json_data, 'msg_id');

        if(!empty($result)){

            //

            $return_data['code']= '100';

            $return_data['msg']= '    ';

            //            

            $return_data['data']= $result;



        }else{

            $return_data['code']= '400';

            $return_data['msg']= '    ';

            $return_data['data']= $json_data;

        }



        //   JSON      

        header('Content-Type:application/json; charset=utf-8');

        //   JSON   

        echo  stripslashes(json_encode($return_data, JSON_UNESCAPED_UNICODE)) ;



    }

 
-------------------------------------------------------------------------------------------------------
json_decode関数の2番目のパラメータTrueとFalseの違い
デフォルト:false、objectオブジェクトを返します
Array( [0] => stdClass Object ( [name] => C51790CC-EDEF-4DF7-9CC7-FE8F4DC8138B&ext=JPG [width] => 200 [height] => 149 )
[1] => stdClass Object ( [name] => 4E6A33AD-D9AE-47CD-9711-E95D9184FBC2&ext=JPG.jpg [width] => 160 [height] => 160 )
)
trueの場合、配列arrayを返します.
Array( [0] => Array ( [name] => C51790CC-EDEF-4DF7-9CC7-FE8F4DC8138B&ext=JPG [width] => 200 [height] => 149 )
[1] => Array ( [name] => 4E6A33AD-D9AE-47CD-9711-E95D9184FBC2&ext=JPG.jpg [width] => 160 [height] => 160 )
)
 
公式解釈:
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode-JSON形式の文字列を符号化する
説明
mixed  json_decode ( string  $json  [, bool  $assoc  = false [, int  $depth  = 512 [, int  $options  = 0 ]]] )
JSON形式の文字列を受け入れてPHP変数に変換
パラメータ
  json
復号されるjson string形式の文字列.
This function only works with UTF-8 encoded data. assoc
パラメータがTRUEの場合、arrayではなくobjectが返されます.depth
User specified recursion depth. options
Bitmask of JSON decode options. Currently only  JSON_BIGINT_AS_STRING  is supported (default is to cast large integers as floats)
戻り値
Returns the value encoded in  json  in appropriate PHP type. Values true, false and null (case-insensitive) are returned as  TRUEFALSE  and NULL  respectively.  NULL  is returned if the  json  cannot be decoded or if the encoded data is deeper than the recursion limit.