PHP跳过相同值上的foreach循环
我在做一个简单的价格比较,我希望输出尽可能干净。我的数据的基本数组如下所示:PHP跳过相同值上的foreach循环,php,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":
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),不要创建另一行,而是在该行中创建另一个包含数据的select。因此,对于同一家商店,没有新行,而是在“金额/价格”下有另一个简单的选择字段,其中包含相关数据。我试图找出一个好的逻辑来解决这个问题。惰性解决方案:
<?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 } ?>
如果(!in_数组($key,$outputKeys)){
$outputKeys[]=$key;
#国家
#付款
?>
数组是按shopid排序的吗?请重新格式化您的代码,这样我们就可以在不歪着头的情况下阅读它…有点离题了,但是在构建选择列表的循环中,您没有打开的
标记。为什么您手动构建json,只是为了将其解码回数组/对象?您正在编写php-为什么不直接构建对象?@Andrew我认为只凭身份证订货更有意义。