2016-10-05 9 views
1

私はこの配列を持っています。 json print_r($json_array);で値を取得していますが、この配列をソートしたいのですが、配列を呼び出す方法がわかりません。私は自分のWebサービスの結果をソートしようとしています。私は投稿した配列結果を取得しています。また、金利の最小値と最大値を使用しました。昇順にソートしたいjavascriptで並べ替える配列

ソートするためにドロップダウンオプション配列を選択したいと思います。レートタイプを選択している場合と同様に、結果はアルファベットのレートタイプを表示します。

HTML

<select id="sort-by" class="sort-by" name="sortBy">      
    <option value="1">Bank/Product Name</option> 
    <option value="2">Interest Rate (% p.a.)</option> 
    <option id="financing" value="3">Financing (in %)</option> 
    <option value="4">Maximum Loan Amount</option> 
    <option value="5">Max Tenure (In Years)</option> 
    <option value="6" onclick="myFunction()" id="rateType" >Interest Rate Type</option> 
</select> 

のWebサービスを介して生成されたアレイは

Array (
    [0] => Array ( 
      [rateType] => Fixed 
      [interestRateMin] => 12.0 
      [financingPercentageMax] => 80 
      [interestRateMax] => 12.0 
      [financingPercentageMin] => 60 
      [bankName] => Muthoot Finance 
      [repaymentTenureInYears] => 0.25 [age] => 32 
      [maxLoanAmt] => 10000000 
     ) 
    [1] => Array ( 
      [rateType] => Fixed 
      [interestRateMin] => 12.0 
      [financingPercentageMax] => 93 
      [interestRateMax] => 24.0 
      [financingPercentageMin] => 88 
      [bankName] => Manappuram Finance 
      [repaymentTenureInYears] => 1 
      [maxLoanAmt] => 10000000 
     ) 
    [2] => Array ( 
      [rateType] => Floating 
      [interestRateMin] => 13.75 
      [financingPercentageMax] => 80 
      [interestRateMax] => 13.75 
      [financingPercentageMin] => 80 
      [bankName] => South Indian Bank- Gold Power 
      [repaymentTenureInYears] => 1 [age] => 32 
      [maxLoanAmt] => 10000000 
     ) 
    [3] => Array ( 
      [rateType] => Floating 
      [interestRateMin] => 14.0 
      [financingPercentageMax] => 90 
      [interestRateMax] => 14.0 
      [financingPercentageMin] => 90 
      [bankName] => HDFC Bank - Gold Loan 
      [repaymentTenureInYears] => 1 
      [maxLoanAmt] => 10000000 
     ) 
    [4] => Array ( 
      [rateType] => Floating 
      [interestRateMin] => 14.0 
      [financingPercentageMax] => 80 
      [interestRateMax] => 14.0 
      [financingPercentageMin] => 80 
      [bankName] => South Indian Bank- Gold rush 
      [repaymentTenureInYears] => 1 
      [maxLoanAmt] => 10000000 
     ) 
) 

PHP

<?php 
if($_POST["occupation"] == '1'){ 
    $occupation = 'Salaried'; 
} 
else{ 
    $occupation = 'Self+Employed'; 
} 

$url = 'http://ppp.psp.com/ajaxv2/getCompareResults.html?rateType='.$_POST["rateType"].'&occupation='.$_POST["occupation"].'&offeringTypeId='.$_POST["offeringTypeId"].'&city='.$_POST["city"].'&loanAmt='.$_POST["loanAmt"].'&q='.$_POST["q"].'&productId='.$_POST["product_id"].'&age='.$_POST["age"]; 
//echo $url; 
// Initiate curl 
$ch = curl_init(); 
// Disable SSL verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch, CURLOPT_URL,$url); 
// Execute 
$result=curl_exec($ch); 
// Closing 
curl_close($ch); 
$json = json_decode($result, true); 

//print_r($json); 
//echo $json['resultList']; 
//echo $json['resultList']['interestRateMin']; 
$json_array = $json['resultList']; 

//print_r($json_array); 

?> 

     <?php if(count($json['resultList']) > 0): ?> 
     <?php 
     foreach ($json['resultList'] as $key=>$value) { 
      if($json["resultList"][$key]["interestRateMin"] == $json["resultList"][$key]["interestRateMax"]){ 
       $interest = $json["resultList"][$key]["interestRateMin"]; 
      } 
      else{ 
       $interest = $json["resultList"][$key]["interestRateMin"].' - '.$json["resultList"][$key]["interestRateMax"]; 

} 

      if($json["resultList"][$key]["financingPercentageMin"] == $json["resultList"][$key]["financingPercentageMax"]){ 
       $financing = $json["resultList"][$key]["financingPercentageMin"]; 
      } 
      else{ 
       $financing = $json["resultList"][$key]["financingPercentageMin"].' - '.$json["resultList"][$key]["financingPercentageMax"]; 

} 

       echo ' 
    <div class="cr-content">     
       <div class="bank-rates"> 
     <ul> 
    <li> 
    <div class="innr-spl" style="padding: 5px 0;"> 
      <span style="display:block;"><img src="'.$json["resultList"][$key]["imageUrl"].'"></span> 
    <span class="hide">'.$json["resultList"][$key]["bankName"].'</span> 
    </div> 
    </li> 

<li><div class="innr-spl2">' . $interest . '%' . '</div></li> 
<li><div class="innr-spl2">' . $financing . '</div></li> 
    <li class="hide"><div class="innr-spl3 kmore" id="'.$key.'"><i class="fa fa-chevron-circle-down" aria-hidden="true"></i>View Datails</div> </li> 
     </ul> 
     <div style="clear:both"></div> 

     <ul class="details" id="ban_'.$key.'"> 
     <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Maximum Loan Amount : '.$json["resultList"][$key]["maxLoanAmt"].'</li> 
     <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Max Tenure (In Years) : '.$json["resultList"][$key]["repaymentTenureInYears"].'</li> 
     <li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Interest Rate Type : '.$json["resultList"][$key]["rateType"].'</li>  
     </ul> 

    </div> 
    </div> 

       '; 
       $i++; 
     } 

    ?> 
<?php endif;?> 

答えて

0

あなたは、数値や文字列の並べ替えについては、列とそのソートスキーマのためのいくつかのヘルパーでローカルにデータを並べ替えることができます。 Thicはソートの降順にも拡張できます。

function sort(key) { 
 
    var sortTypes = { 
 
      string: function (a, b) { return a[key].localeCompare(b[key]); }, 
 
      number: function (a, b) { return a[key] - b[key]; } 
 
     }, 
 
     keys = { 
 
      rateType: 'string', 
 
      interestRateMin: 'number', 
 
      financingPercentageMax: 'number', 
 
      interestRateMax: 'number', 
 
      financingPercentageMin: 'number', 
 
      bankName: 'string', 
 
      repaymentTenureInYears: 'number', 
 
      age: 'number', 
 
      maxLoanAmt: 'number', 
 
     }; 
 

 
    return sortTypes[keys[key]]; 
 
} 
 

 
var data = [{ rateType: 'Fixed', interestRateMin: 12.0, financingPercentageMax: 80, interestRateMax: 12.0, financingPercentageMin: 60, bankName: 'Muthoot Finance', repaymentTenureInYears: 0.25, age: 32, maxLoanAmt: 10000000 }, { rateType: 'Fixed', interestRateMin: 12.0, financingPercentageMax: 93, interestRateMax: 24.0, financingPercentageMin: 88, bankName: 'Manappuram Finance', repaymentTenureInYears: 1, maxLoanAmt: 10000000 }, { rateType: 'Floating', interestRateMin: 13.75, financingPercentageMax: 80, interestRateMax: 13.75, financingPercentageMin: 80, bankName: 'South Indian Bank- Gold Power', repaymentTenureInYears: 1, age: 32, maxLoanAmt: 10000000, }, { rateType: 'Floating', interestRateMin: 14.0, financingPercentageMax: 90, interestRateMax: 14.0, financingPercentageMin: 90, bankName: 'HDFCBank - Gold Loan', repaymentTenureInYears: 1, maxLoanAmt: 10000000 }, { rateType: 'Floating', interestRateMin: 14.0, financingPercentageMax: 80, interestRateMax: 14.0, financingPercentageMin: 80, bankName: 'South Indian Bank- Gold rush', repaymentTenureInYears: 1, maxLoanAmt: 10000000 }]; 
 

 
document.addEventListener('change', function() { 
 
    var select = document.getElementById('sort-by'), 
 
     index = select.selectedIndex, 
 
     key = select.options[index].value 
 

 
    data.sort(sort(key)); 
 
    document.getElementById('out').innerHTML = JSON.stringify(data, 0, 4); 
 
});
<select id="sort-by" class="sort-by" name="sortBy"> 
 
    <option value="bankName">Bank/Product Name</option> 
 
    <option value="interestRateMin">Interest Rate (% p.a.)</option> 
 
    <option value="financingPercentageMax">Financing (in %)</option> 
 
    <option value="maxLoanAmt">Maximum Loan Amount</option> 
 
    <option value="repaymentTenureInYears">Max Tenure (In Years)</option> 
 
    <option value="rateType" id="rateType">Interest Rate Type</option> 
 
</select> 
 
<pre id="out"></pre>

+0

var data = [{}]これは取得していない – user6924814

+0

これは空のjson(オブジェクト)で配列を作成します。 –

+0

@ user6924814、何らかの形でクライアント側にデータを取得しますか?それを並べ替えて、テーブルや他のスタイルに戻して表示することができます。 –

1

スニペットの下に確認してください。私はit.Currently配列の例では、 'rateType'と 'interestRateMin'でソートすることができます共有している。また、最後のドロップダウンオプションまたは2番目のドロップダウンオプションを選択することで、作業デモで確認できます。

var json_array = [{rateType:'Fixed', interestRateMin:12.0}, 
 
        {rateType:'Floating', interestRateMin:11.5}, 
 
        {rateType:'Fixed', interestRateMin:11.8}, 
 
        {rateType:'Floating', interestRateMin:10.5}]; 
 

 
// sort on key values 
 
function keysrt(key,desc) { 
 
    return function(a,b){ 
 
    return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]); 
 
    } 
 
} 
 

 
function sortArray(el){ 
 
    var sortType = el.value; 
 
    var sortKey = ''; 
 
    if(sortType=='6'){ 
 
    sortKey = 'rateType'; 
 
    }else if(sortType=='2'){ 
 
    sortKey = 'interestRateMin'; 
 
    } 
 
    json_array.sort(keysrt(sortKey)); 
 
    console.log(json_array); 
 
}
<select id="sort-by" class="sort-by" name="sortBy" onchange="sortArray(this)"> 
 
    <option value="1">Bank/Product Name</option> 
 
    <option value="2">Interest Rate (% p.a.)</option> 
 
    <option id="financing" value="3">Financing (in %)</option> 
 
    <option value="4">Maximum Loan Amount</option> 
 
    <option value="5">Max Tenure (In Years)</option> 
 
    <option value="6">Interest Rate Type</option> 
 
</select>

+0

私はjson_arrayエラー – user6924814

+0

が定義されていない取得していますあなたのjson_arrayを共有していただけますか? –

+0

ページをリロードし、ソートされた値を表示します。しかし、それは起こっていません – user6924814