如何在PHP中使用foreach只显示一次重复数据?

时间:2022-10-25 19:53:23

I want to select some data from a cross server using PHP foreach and curl by pass it to a table.

我想通过使用PHP foreach和curl从交叉服务器中选择一些数据,将其传递给表。

I want to query the result of draw two times per day example: from

我想每天查询两次绘制的结果示例:from

2015-12-08 13:00 and

2015-12-08 13:00和

2015-12-08 18:00 and I will display it as the correct images如何在PHP中使用foreach只显示一次重复数据? But what I got from this function is like this 如何在PHP中使用foreach只显示一次重复数据?

2015-12-08 18:00我将它显示为正确的图像但我从这个功能得到的是这样的

public function getNumber() {

    $url = "http://103.5.126.24/PalmHallServer_kl/coodraw/coodraw!queryLuckNumberRecordByPeriods.action";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: Json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $contents = curl_exec($ch);
    if($contents){
        echo $contents;
    }else{
        return FALSE;
    }
}

Here is Ajax

这是Ajax

 $.get('<?PHP echo base_url('getnumber'); ?>', function (ldata, status) {

            var $json = $.parseJSON(ldata);
            $("#results").html("");

            var result = [];
            $.each(eval($json), function (i, val) {
                if (val.productName == 'DRAW1') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW2') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW3') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW4') {
                    result = val.luckNumber;
                }
                var table = '<td>' + result + '</td>';
                $(table).appendTo('#results');
            });
        });

This is data aft There is alot of result but I selected only a day You can see on this link

这是数据尾部有很多结果,但我只选择了一天你可以在这个链接上看到

[http://103.5.126.24/PalmHallServer_kl/coodraw/coodraw!queryLuckNumberRecordByPeriods.action][3]
[{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:09","id":100005553,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"22522","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11111,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW1","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW1","productDesc":"Draw1","productEndTime":20990119163000,"productId":11111,"productType":11,"rate":0,"resTmplProductId":11111,"stopTime":900,"validPeriod":2},"productName":"DRAW1","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:01:22","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:15","id":100005554,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"29848","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11112,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW2","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW2","productDesc":"Draw2","productEndTime":20990119163000,"productId":11112,"productType":11,"rate":0,"resTmplProductId":11112,"stopTime":900,"validPeriod":2},"productName":"DRAW2","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:02:32","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:18","id":100005555,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"35495","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11113,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW3","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW3","productDesc":"Draw3","productEndTime":20990119163000,"productId":11113,"productType":11,"rate":0,"resTmplProductId":11113,"stopTime":900,"validPeriod":2},"productName":"DRAW3","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:03:27","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:21","id":100005556,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"89346","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11114,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW4","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW4","productDesc":"Draw4","productEndTime":20990119163000,"productId":11114,"productType":11,"rate":0,"resTmplProductId":11114,"stopTime":900,"validPeriod":2},"productName":"DRAW4","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:04:26","winAmount":0}]

1 个解决方案

#1


1  

Change the AJAX code to following:

将AJAX代码更改为以下内容:

$("#results").html("");

var result = [];
var table = '';
tmpDRAW1 = '';
tmpDRAW2 = '';
tmpDRAW3 = '';
tmpDRAW4 = '';
tmpCounter = 0;

$.each(eval($json), function(i, val) {
  tmpOpenTime = val.lotteryOpenTime;

  if (val.productName == 'DRAW1') {
    tmpDRAW1 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW2') {
    tmpDRAW2 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW3') {
    tmpDRAW3 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW4') {
    tmpDRAW4 = val.luckNumber;
    tmpCounter++;
  }

  if (tmpCounter == 4) {
    table += '<tr><td>' + tmpOpenTime + '</td>';
    table += '<td>' + tmpDRAW1 + '</td>';
    table += '<td>' + tmpDRAW2 + '</td>';
    table += '<td>' + tmpDRAW3 + '</td>';
    table += '<td>' + tmpDRAW4 + '</td></tr>';
    tmpCounter = 0;
  }

});
$(table).appendTo('#results');
});

This does the output format you want, and is resistant to ordering of DRAWs in your JSON.

这样可以处理您想要的输出格式,并且可以抵抗JSON中对DRAW的排序。

#1


1  

Change the AJAX code to following:

将AJAX代码更改为以下内容:

$("#results").html("");

var result = [];
var table = '';
tmpDRAW1 = '';
tmpDRAW2 = '';
tmpDRAW3 = '';
tmpDRAW4 = '';
tmpCounter = 0;

$.each(eval($json), function(i, val) {
  tmpOpenTime = val.lotteryOpenTime;

  if (val.productName == 'DRAW1') {
    tmpDRAW1 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW2') {
    tmpDRAW2 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW3') {
    tmpDRAW3 = val.luckNumber;
    tmpCounter++;
  }
  if (val.productName == 'DRAW4') {
    tmpDRAW4 = val.luckNumber;
    tmpCounter++;
  }

  if (tmpCounter == 4) {
    table += '<tr><td>' + tmpOpenTime + '</td>';
    table += '<td>' + tmpDRAW1 + '</td>';
    table += '<td>' + tmpDRAW2 + '</td>';
    table += '<td>' + tmpDRAW3 + '</td>';
    table += '<td>' + tmpDRAW4 + '</td></tr>';
    tmpCounter = 0;
  }

});
$(table).appendTo('#results');
});

This does the output format you want, and is resistant to ordering of DRAWs in your JSON.

这样可以处理您想要的输出格式,并且可以抵抗JSON中对DRAW的排序。