2016-05-19 10 views
-1

私は単純な値段で作業し、できるだけ出力をきれいにします。私のデータの基本的な配列は次のようになります。まずPHP foreachループを同じ値にスキップします

Array ([0] => stdClass Object ([id] => 84 [system_id] => 174 [url] => https://firstshop-pricecheck.com/url-1 [shopid] => First Shop [price] => {"price": "32 00", "amount": "10", "variation": "standard"} [currency] => EUR) [1] => stdClass Object ([id] => 85 [system_id] => 174 [url] => https://firstshop-pricecheck.com/url-2 [shopid] => First Shop [price] => {"price": "18 00", "amount": "3", "variation": "other"}, {"price": "28 01", "amount": "5", "variation": "standard"}, {"price": "49 00", "amount": "10", "variation": "other"}, {"price": "108 00", "amount": "25", "variation": "standard"} [currency] => EUR) [2] => stdClass Object ([id] => 106 [system_id] => 174 [url] => https://secondshop-pricecheck.com/url-1 [shopid] => Second Shop [price] => {"price": "3 25", "amount": "1", "variation": "standard"}, {"price": "4 50", "amount": "1", "variation": "other"}, {"price": "32 50", "amount": "10", "variation": "standard"}, {"price": "45 00", "amount": "10", "variation": "other"} [currency] => GBP)) 

、私たちはテーブルのヘッダーがあります。現時点では

<table id="pricecheck" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
       <thead> 
        <tr> 
         <th>Shop</th> 
         <th>Country</th> 
         <th>Payment</th> 
         <th>Amount/Price</th> 
        </tr> 
       </thead> 
       <tbody> 

を、新しい行は、すべてのお店(shopid)のためにそこにある、中列 "Amount/Price"私は単純な選択フィールドにデータを置く別のforeachを持っています。

<?php foreach ($array as $key => $value) { ?> 
         <tr> 
         <td><?php echo $value->shopid; ?></td> 
         <td>#Country</td> 
         <td>#Payment</td> 
         <td> 
         <?php 
         # decode data to json: 
         $xmyson = $value->price . ', '; 
         $ymyson = rtrim($xmyson, ', '); 
         $zmyson = '{ "prices": [' . $ymyson . ']}'; 

         $decode = json_decode($zmyson); 
         ?> 
         <select id="price_check_ordering" name="price_check_ordering" class="inputbox"> 
          <?php 
          foreach ($decode->prices as $xvalue) { ?> 
          <option> 
          <?php 
          $fprice = str_replace(" ", ",", $xvalue->price); 
          echo $fprice . ' €'; 
          ?> 
          </option> 
          <?php } ?> 
          </select> 
          ?> 
         </td> 
         </tr> 
            <?php } ?> 

は今、最初のループのために: - しかし、データと、その行に別の選択を作成し、それは同じ店(shopid)だ場合は、別の行を作成してはいけません。したがって、同じ店舗の新しい列はありませんが、「金額/価格」の下に相対的なデータを持つ別の単純な選択フィールドがあります。私はこれを解決する良い論理を理解しようとします。

+0

はshopid順の配列ですか? –

+1

私たちの頭を傾けずに読むことができるようにコードを再フォーマットしてください... – Edward

+1

少し話題がありますが、選択リストを構築するループ内に '

答えて

0

レイジー・ソリューション:

<?php 

//order array by id 
usort($your_data, function($a, $b) { 
    return $a->id > $b->id; 
}); 

$outputKeys = []; 
foreach ($array as $key => $value) { ?> 
    if(!in_array($key, $outputKeys)){ 
     $outputKeys[] = $key; 
     <tr> 
        <td><?php echo $value->shopid; ?></td> 
        <td>#Country</td> 
        <td>#Payment</td> 
        <td> 
        <?php 
        # decode data to json: 
        $xmyson = $value->price . ', '; 
        $ymyson = rtrim($xmyson, ', '); 
        $zmyson = '{ "prices": [' . $ymyson . ']}'; 

        $decode = json_decode($zmyson); 
        ?> 
        <select id="price_check_ordering" name="price_check_ordering" class="inputbox"> 
         <?php 
       } //endif output your select 
         foreach ($decode->prices as $xvalue) { ?> 
         <option> 
         <?php 
         $fprice = str_replace(" ", ",", $xvalue->price); 
         echo $fprice . ' €'; 
         ?> 
         </option> 
         <?php } ?> 
         </select> 
         ?> 
        </td> 
        </tr> 
           <?php } ?> 
関連する問題