array_chunk并发送foreach循环中的所有部分不起作用

时间:2019-11-28 07:08:20

标签: php arrays foreach numbers

我有一个包含2000多个项目的手机号码数组:

$mobiles = array("number 1, number 2, number 3, ...)

我想将此数组拆分为100个细分,然后在每个数组中执行: 看到我的代码:

$username='xxxxxxx';
    $password='xxxxxx';
    $sendNumber='1000100';
    $message= 'text text';
    $client=new SoapClient("http://example.com/services/smsService.svc?wsdl");
    //spilit mobile number to 100 segments
    $spilited_mobiles = array_chunk($mobiles,100,true);
    foreach ($spilited_mobiles as $numbers){
        $params=array('username'=>$username,'password'=>$password,
    'senderNumber'=>$sendNumber,'recipientNumbers'=>$numbers,'message'=>$message,'saveType'=>1);
    $result=$client->sendMessage($params);
    }

问题是它只发送前100个号码,而循环中没有执行其他号码 谢谢

2 个答案:

答案 0 :(得分:0)

您需要添加另一个foreach来循环播放100。

$numbers是一个包含100个手机号码的数组。

'recipientNumbers'=>$numbers

解决方案

$spilited_mobiles = array_chunk($mobiles, 100, TRUE);
foreach ($spilited_mobiles as $numbers){

    //Loop the 100
    foreach($numbers as $number){
            $params=array('username'=>$username,'password'=>$password,
            'senderNumber'=>$sendNumber,'recipientNumbers'=>$number,'message'=>$message,'saveType'=>1);
            $result=$client->sendMessage($params);
    }
}

您可以尝试示例代码here

答案 1 :(得分:0)

您需要添加两个foreach来执行此操作:

$mobiles = array(12, 123, 1234);
$username='xxxxxxx';
$password='xxxxxx';
$sendNumber='1000100';
$message= 'text text';
$client=new SoapClient("http://example.com/services/smsService.svc?wsdl");
$spilited_mobiles = array_chunk($mobiles,100,true);
foreach ($spilited_mobiles as $numbers){
  foreach($numbers as $number){
    $params=array('username'=>$username,'password'=>$password,
    'senderNumber'=>$sendNumber,'recipientNumbers'=>$number,'message'=>$message,'saveType'=>1);
    $result=$client->sendMessage($params);
  }
}