PHP parsing json two-dimensional array string

Source: Internet
Author: User

Data in the client's request body: [{"msg_id": 1, "Msg_status": "Has_read"}, {"msg_id": 2, "Msg_status": "Has_read"}] is a two-dimensional array string
$json _data = Json_decode ($GLOBALS [' Http_raw_post_data '], true);
In fact, with this sentence can be implemented JSON two-dimensional array string to PHP two-dimensional array variable, do not have to dynamically construct a two-dimensional array
The second parameter of the function is important: the output of the PHP object is not true, and the output of the PHP array is true.


   /** * Bulk update of message status based on uploaded message ID collection*/     Public functionUpdate_status_batch () {//data that needs to be updated/*$data = Array (' msg_id ' = 1, ' msg_status ' = ' has_read '            '), Array (' msg_id ' = 2, ' msg_status ' = ' has_read ') )        );*/        //The return value is the JSON object by default, and when the second optional argument is true, the array is returned, and if it is a JSON string for a two-dimensional array, it is converted to a two-dimensional array of PHP variables .        $json _data= Json_decode ($GLOBALS[' Http_raw_post_data '],true);/*//number of elements//$item _num = count ($json _data);        Defines a two-dimensional array $array = array ();                foreach ($json _data as $item) {$array _unit = array (' msg_id ' = = $item->msg_id,            ' Msg_status ' = $item->msg_status);        Append element Array_push ($array, $array _unit) to a two-dimensional array; }*/        //Update, the return value is the number of record bars affected by the update        $result=$this->m_user_msg->update_batch ($json _data, ' msg_id '); if(!Empty($result)){            //if it is not empty, the return succeeds            $return _data[' Code ']= ' 100 '; $return _data[' msg ']= ' processing success '; //string-to-numeric processing is required            $return _data[' Data ']=$result; }Else{            $return _data[' Code ']= ' 400 '; $return _data[' msg ']= ' processing failed '; $return _data[' Data ']=$json _data; }        //settings are returned to the requester in JSON        Header(' Content-type:application/json; Charset=utf-8 '); //convert to JSON string        Echo  stripslashes(Json_encode ($return _data,Json_unescaped_unicode))    ; }

PHP parsing json two-dimensional array string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.