MYSQL连接并创建不同的json

MYSQL连接并创建不同的json,mysql,json,Mysql,Json,我有以下选择 if ($result = $mysqli->query("SELECT a.quoteID, a.quoteTitle , a.notes, c.web_path FROM quotes a INNER JOIN users_files b on b.user_id = a.quoteID INNER JOIN files c on c.id = b.file_id ORDER BY quoteID ASC")) { $tempArray = a

我有以下选择

if ($result = $mysqli->query("SELECT 
a.quoteID, a.quoteTitle , a.notes,  c.web_path 
FROM quotes a 
INNER JOIN users_files b on b.user_id = a.quoteID 
INNER JOIN files c on c.id = b.file_id ORDER BY quoteID ASC")) 



{

    $tempArray = array();
    while($row = $result->fetch_object()) {
            $tempArray = $row;
            array_push($myArray, $tempArray);
        }
    echo json_encode($myArray);
}
这让我得到了我需要的信息

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/22.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/21.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"},{"quoteID":"8","quoteTitle":"Bryar2","notes":"Long","web_path":"\/surplusAdmin3\/upload\/17.png"},{"quoteID":"11","quoteTitle":"Avram","notes":"Allison","web_path":"\/surplusAdmin3\/upload\/4.jpg"}]
我现在需要得到以下结果:

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path1":"\/surplusAdmin3\/upload\/23.pdf","web_path2":"\/surplusAdmin3\/upload\/22.jpeg","web_path3":"\/surplusAdmin3\/upload\/21.jpeg","web_path4":"\/surplusAdmin3\/upload\/20.jpeg"}]
我不想重做MYSQL语句,我只需要以我需要的格式创建一个新数组

我有以下代码:

var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
    console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+  fileCount);
    if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
  else
    {
        console.log('just add the filename array Row = ' +arrayRow );
        // need to insert to the array arrayRow
        // dataChanged.push('web_path'+fileCount,e["web_path"]);
        toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];

        var field = 'web_path'+fileCount;
        var data = e["web_path"];
        var tempArray = [field,data];
        dataChanged.push(toPush);
        dataChanged.field = data;
        //dataChanged = dataChanged.concat(tempArray);
        fileCount = fileCount +1;
    }
    currentQuoteID = e["quoteID"];
   console.log('stuff '+ JSON.stringify(dataChanged));
});
我得到的结果是:

dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image"},"web_path1","/surplusAdmin3/upload/22.jpeg"]
我需要的是

dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image","web_path1","/surplusAdmin3/upload/22.jpeg"}]

但我无法在这里锻炼我需要做的事情。

如下所示:

// loop through and add field 'Type' can all be done in one loop.
    dataReturned.forEach(function(e){
    if (typeof e === "object" ){
    e["Type"] = "other";

   // console.log('stuff '+e['Type'][i] )
}

});

//loop through and workout the file type
    dataReturned.forEach(function(e){
    if (typeof e === "object" ){
    var ext = e['web_path'].substr(e['web_path'].lastIndexOf('.') + 1);
    if ( (ext === 'jpeg' ) || ( ext === 'jpg' )|| ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))

    { e["Type"] = "image"; }
    //console.log(e['web_path']);
   // console.log('stuff '+ JSON.stringify(e))
  }

});
// create a new array formated as needed
var currentQuoteID =0;
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
e["quoteID"] + 'file count = '+  fileCount);
    if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e);     arrayRow = arrayRow + 1; fileCount = 1}
  else
    {
        //console.log('just add the filename array Row = ' +arrayRow );
        // need to insert to the array arrayRow
        // dataChanged.push('web_path'+fileCount,e["web_path"]);
        var field = 'web_path'+fileCount;
        var data = webPath+e["web_path"];
        var tempArray = [field,data];
        dataChanged[arrayRow]['web_path'+fileCount] = data
        fileCount = fileCount +1;
    }
    currentQuoteID = e["quoteID"];
   console.log('stuff '+ JSON.stringify(dataChanged));
});

查看您预期的JSON结果,我认为您将需要某种类型的数据透视,但我不想对结果集进行反向工程。见: