解码json响应并为每个插入使用

<?php
$string ='{"type":"Text","totalprice":"0.05","totalgsm":"6","remaincredit":"63.6433","messages": [
        {"status":"1","messageid":"157157","gsm":"9211111111111"},
        {"status":"1","messageid":"157154","gsm":"9211111111112"},
        {"status":"1","messageid":"157151","gsm":"9211111111113"},
        {"status":"1","messageid":"157149","gsm":"9211111111114"},
        {"status":"1","messageid":"157142","gsm":"9211111111115"},
        {"status":"1","messageid":"157130","gsm":"9211111111116"}
        ]}';
?>

I want to get:

type            = text
totalprice      = 0.05
totalgsm        = 6
remainingcredit = 63.6433

status=1 messageid=157157 gsm=9211111111111
status=1 messageid=157157 gsm=9211111111112
status=1 messageid=157157 gsm=9211111111113
status=1 messageid=157157 gsm=9211111111114

I want to insert in MYSQLDB in 2 queries:

First INSERT Query for type = text, totalprice = 0.05, totalgsm = 6, remainingcredit = 63.6433

Second INSERT Query for messages array Using foreach to insert all given

status=1 messageid=157157 gsm=9211111111111
status=1 messageid=157157 gsm=9211111111112
status=1 messageid=157157 gsm=9211111111113
status=1 messageid=157157 gsm=9211111111114

Please Guide me how to decode this type of json response and how to parse to insert in DB?

May this way you have to do:

$string = json_decode('{"type":"Text","totalprice":"0.05","totalgsm":"6","remaincredit":"63.6433","messages": [
    {"status":"1","messageid":"157157","gsm":"9211111111111"},
    {"status":"1","messageid":"157154","gsm":"9211111111112"},
    {"status":"1","messageid":"157151","gsm":"9211111111113"},
    {"status":"1","messageid":"157149","gsm":"9211111111114"},
    {"status":"1","messageid":"157142","gsm":"9211111111115"},
    {"status":"1","messageid":"157130","gsm":"9211111111116"}
    ]}', true);

$type = $string['type'];
$totalprice = $string['totalprice'];
$totalgsm = $string['totalgsm'];
$remaincredit = $string['remaincredit'];

/** Insert using First Query **/

/** Now for message array **/
foreach ($string['messages'] as $key => $msg) {
    $status = $msg['status'];
    $msg_id = $msg['messageid'];
    $gsm = $msg['gsm'];
    /*** Insert using your second query ***/
    /** ------------------------ **/
}

you can use json_decode() in php to decode this string.

$data = json_decode($string);

I think you are wanting to use json_decode as stated by @myeishvar.

This will get turn your JSON string into a PHP array.

$records = json_decode($string);
print_r($records); // Display the array contents

This will echo the required information for each record in the array.

foreach ($records as $record) {
 echo 'Type: '.$record->type.'<br>';
 echo 'Total Price: '.$record->totalprice.'<br>';
 echo 'Total GSM: '.$record->totalgsm.'<br>';
 echo 'Remaining Credit: '.$record->remainingcredit.'<br>';
 echo '<br>';
}

This is an example of building a query with foreach which you can use to insert values into individual records in a database table.

$sql='INSERT INTO recordtable (type, totalprice, totalgsm, remainingcredit) VALUES ';
foreach ($records as $record) {
    $sql .= '('.$record->$type.', '.$record->$totalprice.', '.$record->$totalgsm.', '.$record->$remainingcredit.'), '
}
...do your SQL query...

I should note that if this data is from user input (a form), it should be properly sanitized before inserting it into the database.