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