亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 array_chunk mysqli 結果一次分成 100 個塊

如何將 array_chunk mysqli 結果一次分成 100 個塊

PHP
胡子哥哥 2023-09-22 17:36:57
有沒有辦法可以對 mysqli 結果進行 Array_chunk,我從表中循環消息,然后將值傳遞到方法“Sms”中。該方法將創建一個 Sms 對象列表,我通過函數 SendBatchSMS 傳遞該列表。我的 API 端點只能允許每個請求 100 次調用。我嘗試過將列表分塊為“$sms”,當我 print_r($sms) 時,它看起來效果很好,但是當回顯響應時,無論 array_chunk 函數中指定的大小如何,它都只返回 48/249 個響應。我的問題是,是否有更好的選擇來實現此目的,例如 array_chunking mysqli 結果而不是數組列表?$query_sch = "SELECT * FROM ct_queue";  $sch_result = mysqli_query($mysqli, $query_sch);$rows[] = mysqli_fetch_array($sch_result);$count = mysqli_num_rows($sch_result);    foreach($sch_result as $value){    $phone = $value['phone'];    $sender = $value['sender'];     $message = $value['message'];     $user_id = $value['user_id'];    $link_id = NULL;    $correlator = 'correlator_string';    $endpoint = 'example.com';    $token = "token_string";        // $list = array();    $version = "v1"; //DONT change unless you are using a different version    $instance = new BonTech($token, $version);    $list[] = new Sms($sender, $phone, $message, $correlator, null, $endpoint);}      $row_chunks = array_chunk($list, 100);      foreach ($row_chunks as $chunk){    $sms = array();    ////////here we have 100 messages on each chunk    ///////Loop through the messages in side the chunk    foreach ($chunk as $row) {        $sms[] = ($row);    }    // print_r($sms);}$response = call_user_func_array(array($instance, "sendBatchSMS"), $sms);$response = json_encode($response, true);$results = json_decode($response, true);print_r($response);
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

您在循環完成$sms后使用。foreach所以它只會包含最后一個塊。您需要在循環內使用它。


也不需要使用循環來復制$chunk到$sms.


mysqli_fetch_array($sch_result)由于在第一個循環之前調用,您還跳過了第一行結果foreach。


$instance似乎不依賴于$value,所以它不應該在foreach循環中。


$query_sch = "SELECT * FROM ct_queue";  

$sch_result = mysqli_query($mysqli, $query_sch);


$list = array();

foreach($sch_result as $value)

{

    $phone = $value['phone'];

    $sender = $value['sender']; 

    $message = $value['message']; 

    $user_id = $value['user_id'];


    $link_id = NULL;

    $correlator = 'correlator_string';

    $endpoint = 'example.com';


    $list[] = new Sms($sender, $phone, $message, $correlator, null, $endpoint);

}


$token = "token_string";

$version = "v1"; //DONT change unless you are using a different version

$instance = new BonTech($token, $version);

  

$row_chunks = array_chunk($list, 100);

      

foreach ($row_chunks as $sms){

    $response = call_user_func_array(array($instance, "sendBatchSMS"), $sms);

    print_r($response);

}


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 106 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號