dongyongju9560 2017-04-11 20:26
浏览 56
已采纳

PHP在Foreach循环中运行卷曲函数

I have to send out SMS in multiples of 150 batch per CURL post. Here is my code,

$mobiles = "9844700001,9844700002,9844700003,9844700004;9844700005
 9844700006,9844700007,9844700008,9844700009,9844700010,.... upto 1000 numbers"
$rmobiles = preg_split('/[\ 
\;\:\,]+/', $mobiles, -1, PREG_SPLIT_NO_EMPTY);
if(count($rmobiles) <= 1000){
    foreach($rmobiles as $key => $value){
        if ($key % 150 == 0) {
            //Generate a new random ID
            $randno = rand(0,10000);
        }
        $this->addtoDB("00000", $value, $randno);
    } 
    return $this->sendmsms($randno);
} else {
    echo "error, max 1000";
}

Since sendmsms function is outside the foreach loop, it only sends out the last batch with the random number and if I include sendmsms function within foreach loop then the function will run as many mobile numbers are there.

How should I able to run sendmsms function based on generated randno and so that my curl function sendmsms will run properly and generate db records genuinely.

Edit

I again tried like this, but its inserting single record in my DB.

    foreach($rmobiles as $key => $value){
        if ($key % 150 == 0) {
            //Generate a new random ID
            $randno = rand(0,10000);
        }
        $this->addtoDB("00000", $value, $randno);
        if ($key % 150 == 0) {
            return $this->sendmsms($randno);
        }
    } 

Edit 2

I even tried creating array for randno. But only first batch is going, not the all 7 batches.

    $arr = array();
    foreach($rmobiles as $key => $value){
        if ($key % 150 == 0) {
            //Generate a new random ID
            $randno = rand(0,10000);
            $arr[] = $randno;
        }
        $this->addtoDB("00000", $value, $randno);
    }
    foreach($arr as $k => $v){
        return $this->sendmsms($v);
    }

When I echo it, it perfectly displays but through DB its going just 1 batch. What could be the reason?

  • 写回答

1条回答 默认 最新

  • dtt78245 2017-04-11 20:46
    关注

    One solution that comes to mind is using array_chunk() to break $rmobiles into multiple arrays with a maximum of 150 elements each. Not sure if your sendmsms function takes a string or an array, but something like this may get you on the right track:

    $mobiles = "9844700001,9844700002,9844700003,9844700004;...";
    $rmobiles = preg_split('/[\ 
    \;\:\,]+/', $mobiles, -1, PREG_SPLIT_NO_EMPTY);
    foreach (array_chunk($rmobiles, 150) as $batch) {
        $this->sendmsms(implode(',',$batch));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?