PHP跳过相同值的foreach循环

时间:2016-05-19 15:33:28

标签: php

我正在进行简单的价格比较,我希望输出尽可能清洁。我的数据的基本数组如下所示:

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)都有一个新行,在“金额/价格”列中我有另一个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),不要创建另一行 - 但是在该行中使用数据创建另一个选择。因此,同一商店没有新行,而是“金额/价格”下的另一个简单选择字段以及相关数据。我试图找出一个很好的逻辑来解决这个问题。

1 个答案:

答案 0 :(得分: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 } ?>