2017-05-18 7 views
0

このコードJSON PHPの整形出力の問題が

<?php 
$connect = mysqli_connect("localhost", "user", "password", "db"); 

$sql = "SELECT * FROM current_rates WHERE date = '2017-05-01'"; 

$result = mysqli_query($connect, $sql); 

$Json_array = array(); 


while ($row = mysqli_fetch_assoc($result)) { 

    $Json_array[] = $row; 

} 

/* echo "<pre>"; 
    print_r($Json_array); 
     echo "<pre>"; 
*/ 
echo json_encode($Json_array); 
?> 

戻り、このJSON情報

[{"key":"47","financial_institution":"ATB Financial","variable_rate":"2.1","six_months":"0","one_year":"2.99","two_year":"2.39","three_year":"2.54","four_year":"2.57","five_year":"2.74","date":"2017-05-01"},{"key":"48","financial_institution":"Alterna Bank","variable_rate":"2.25","six_months":"0","one_year":"2.79","two_year":"2.74","three_year":"2.64","four_year":"2.55","five_year":"2.6","date":"2017-05-01"},{"key":"49","financial_institution":"Alterna Savings","variable_rate":"2.28","six_months":"0","one_year":"2.79","two_year":"2.74","three_year":"2.64","four_year":"2.74","five_year":"2.64","date":"2017-05-01"},{"key":"50","financial_institution":"Bank of Montreal","variable_rate":"2.85","six_months":"0","one_year":"3.09","two_year":"3.19","three_year":"3.59","four_year":"4.09","five_year":"4.84","date":"2017-05-01"},{"key":"51","financial_institution":"Bank of Nova Scotia","variable_rate":"2.7","six_months":"0","one_year":"3.29","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"52","financial_institution":"CIBC Mortgages","variable_rate":"2.7","six_months":"0","one_year":"2.84","two_year":"2.79","three_year":"3.39","four_year":"4.09","five_year":"4.79","date":"2017-05-01"},{"key":"53","financial_institution":"Canadian Western Bank","variable_rate":"2.5","six_months":"0","one_year":"2.44","two_year":"2.29","three_year":"2.44","four_year":"2.54","five_year":"2.64","date":"2017-05-01"},{"key":"54","financial_institution":"Canadian Western Trust","variable_rate":"2.9","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.2","five_year":"4.74","date":"2017-05-01"},{"key":"55","financial_institution":"Comtech Credit Union","variable_rate":"0","six_months":"0","one_year":"2.84","two_year":"2.89","three_year":"2.95","four_year":"2.99","five_year":"2.89","date":"2017-05-01"},{"key":"56","financial_institution":"DUCA Financial Services","variable_rate":"2.5","six_months":"0","one_year":"3.04","two_year":"2.94","three_year":"2.94","four_year":"3.14","five_year":"2.79","date":"2017-05-01"},{"key":"57","financial_institution":"Desjardins - Caisses","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.24","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"58","financial_institution":"Effort Trust","variable_rate":"0","six_months":"0","one_year":"3.35","two_year":"3.5","three_year":"3.7","four_year":"4.3","five_year":"4.35","date":"2017-05-01"},{"key":"59","financial_institution":"Equitable Bank","variable_rate":"0","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"60","financial_institution":"First Calgary Financial","variable_rate":"0","six_months":"0","one_year":"2.89","two_year":"2.89","three_year":"2.64","four_year":"2.79","five_year":"2.74","date":"2017-05-01"},{"key":"61","financial_institution":"First National Financial","variable_rate":"2.35","six_months":"0","one_year":"2.64","two_year":"2.64","three_year":"2.64","four_year":"2.84","five_year":"2.84","date":"2017-05-01"},{"key":"62","financial_institution":"FirstOntario Credit Un.","variable_rate":"2.7","six_months":"0","one_year":"2.79","two_year":"2.49","three_year":"2.64","four_year":"2.79","five_year":"2.89","date":"2017-05-01"},{"key":"63","financial_institution":"HSBC Bank Canada","variable_rate":"2.25","six_months":"0","one_year":"2.25","two_year":"2.29","three_year":"2.39","four_year":"2.49","five_year":"2.49","date":"2017-05-01"},{"key":"64","financial_institution":"Home Trust Company-Agent","variable_rate":"2.35","six_months":"0","one_year":"2.69","two_year":"2.14","three_year":"2.59","four_year":"2.74","five_year":"2.84","date":"2017-05-01"},{"key":"65","financial_institution":"ICICI Bank Canada","variable_rate":"3.05","six_months":"0","one_year":"2.64","two_year":"2.64","three_year":"2.64","four_year":"3.19","five_year":"3.29","date":"2017-05-01"},{"key":"66","financial_institution":"Investors Group Trust","variable_rate":"2.85","six_months":"0","one_year":"3.14","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"67","financial_institution":"Kindred Credit Union","variable_rate":"2.7","six_months":"0","one_year":"3.35","two_year":"3.35","three_year":"3.4","four_year":"3.4","five_year":"2.79","date":"2017-05-01"},{"key":"68","financial_institution":"Laurentian Bank Canada","variable_rate":"2.7","six_months":"0","one_year":"2.89","two_year":"3.04","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"69","financial_institution":"League Svgs & Mortgage","variable_rate":"0","six_months":"0","one_year":"2.99","two_year":"2.59","three_year":"2.64","four_year":"2.74","five_year":"2.79","date":"2017-05-01"},{"key":"70","financial_institution":"Libro Credit Union","variable_rate":"0","six_months":"0","one_year":"2.7","two_year":"2.75","three_year":"2.8","four_year":"2.85","five_year":"2.89","date":"2017-05-01"},{"key":"71","financial_institution":"London Life","variable_rate":"2.85","six_months":"0","one_year":"3.14","two_year":"3.09","three_year":"3.39","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"72","financial_institution":"Manulife Bank","variable_rate":"2.7","six_months":"0","one_year":"3","two_year":"2.94","three_year":"2.84","four_year":"2.89","five_year":"2.89","date":"2017-05-01"},{"key":"73","financial_institution":"Manulife Trust","variable_rate":"0","six_months":"0","one_year":"3","two_year":"2.94","three_year":"2.84","four_year":"2.89","five_year":"2.89","date":"2017-05-01"},{"key":"74","financial_institution":"Meridian Credit Union","variable_rate":"2.3","six_months":"0","one_year":"2.84","two_year":"2.79","three_year":"3.39","four_year":"3.89","five_year":"2.69","date":"2017-05-01"},{"key":"75","financial_institution":"National Bank","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.39","four_year":"3.89","five_year":"4.74","date":"2017-05-01"},{"key":"76","financial_institution":"PACE Savings Credit Un","variable_rate":"0","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"77","financial_institution":"Parama Credit Union","variable_rate":"2.65","six_months":"0","one_year":"2.8","two_year":"2.85","three_year":"2.95","four_year":"3.05","five_year":"3.15","date":"2017-05-01"},{"key":"78","financial_institution":"Peace Hills Trust","variable_rate":"0","six_months":"0","one_year":"3.15","two_year":"3.5","three_year":"3.75","four_year":"3.85","five_year":"3.95","date":"2017-05-01"},{"key":"79","financial_institution":"Rapport Credit Union","variable_rate":"2.85","six_months":"0","one_year":"3.09","two_year":"3.04","three_year":"3.5","four_year":"3.04","five_year":"2.99","date":"2017-05-01"},{"key":"80","financial_institution":"Royal Bank of Canada","variable_rate":"2.7","six_months":"0","one_year":"3.14","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"81","financial_institution":"Steinbach Credit Union","variable_rate":"0","six_months":"0","one_year":"2.39","two_year":"2.39","three_year":"2.49","four_year":"2.59","five_year":"2.69","date":"2017-05-01"},{"key":"82","financial_institution":"Street Capital Bank","variable_rate":"2.15","six_months":"0","one_year":"2.69","two_year":"2.69","three_year":"2.69","four_year":"2.69","five_year":"2.69","date":"2017-05-01"},{"key":"83","financial_institution":"T-D Mortgage","variable_rate":"2.85","six_months":"0","one_year":"3.04","two_year":"2.84","three_year":"3.44","four_year":"3.89","five_year":"4.64","date":"2017-05-01"},{"key":"84","financial_institution":"Tangerine","variable_rate":"2.3","six_months":"0","one_year":"3.19","two_year":"3.24","three_year":"3.49","four_year":"3.69","five_year":"2.69","date":"2017-05-01"},{"key":"85","financial_institution":"WFCU Credit Union","variable_rate":"0","six_months":"0","one_year":"3","two_year":"3.04","three_year":"3.65","four_year":"4.39","five_year":"4.64","date":"2017-05-01"},{"key":"86","financial_institution":"Your Neighbourhood C.U.","variable_rate":"0","six_months":"0","one_year":"2.79","two_year":"2.89","three_year":"3.09","four_year":"3.15","five_year":"2.79","date":"2017-05-01"}] 

私は私正しいJSON形式だと思う、このデータを仕事にしようとしている...しかし、私はこれは最初のオブジェクトが記載されていないので、私は間違っていると思っている

オブジェクトに名前を付けるようにコードを編集する方法がわかりません(多分オブジェクトは間違った用語です - 私は非常に新しく、学習はこの素晴らしいサイトとyouTubeから行っています)プットを追加し、以下の貸し手のような「lable」を持っている必要がありますから...私はアイデアを持っているあなたのJSONは、フロントにはラベルが存在しないにもかかわらず、すでに有効です。しかし、あなたがしたい場合はこれを行う方法。

{ 
"lenders": [ 
    { 
    "date": "2017-05-01", 
    "financial_institution": "Your Neighbourhood C.U.", 
    "five_year": "2.79", 
    "four_year": "3.15", 
    "key": "86", 
    "one_year": "2.79", 
    "six_months": "0", 
    "three_year": "3.09", 
    "two_year": "2.89", 
    "variable_rate": "0" 
    }, 

答えて

1

あなたのjsonをラベルで構成する方法は次のとおりです(最後の2行を見てください)

<?php 
$connect = mysqli_connect("localhost", "user", "password", "db"); 

$sql = "SELECT * FROM current_rates WHERE date = '2017-05-01'"; 

$result = mysqli_query($connect, $sql); 

$Json_array = array(); 

while ($row = mysqli_fetch_assoc($result)) { 
    $Json_array[] = $row; 
} 
/* echo "<pre>"; 
    print_r($Json_array); 
     echo "<pre>"; 
*/ 

$finalJson['lenders'] = $Json_array; 
echo json_encode($finalJson); 
?> 
+0

ありがとう。ラベルを付けることで、私が参照ラベルを見つけたチュートリアルの数をより簡単にすることができます。乾杯、 –

+0

完全に同意します。 – ayip

+0

中間変数を省略することもできます: 'json_encode(array( 'lenders' => $ Json_array))' – tadman