Edward Muss
Edward Muss

Reputation: 49

How pass all values outside the loop to call_user_func_array in PHP

I am passing the $instance to call_user_func_array and SendBatchSms function, however the $response is only using the last $token in the loop, extending the loop works well but it is slow, how can I pass the $instance to call_user_func_array function with all loop values and not the last one. I am new to php

foreach($result as $row){
    $token = $row['token'];
    $version = "v1"; //DONT change unless you are using a different version
    $instance = new BongaTech($token, $version);
    $list[] = new Sms($sender, $phone, $message, $correlator, null, $endpoint);
}


//   $list = null;
  if($list !== null){ 

  $row_chunks = array_chunk($list, 100);
  ////////here we have 100 messages on each chunk
    ///////Loop through the messages in side the chunk
    foreach ($row_chunks as $sms){
    
        $response = call_user_func_array(array($instance, "sendBatchSMS"), $sms);
        
        $response = json_encode($response, true);
        $results = json_decode($response, true);
        print_r($response);
}

Upvotes: 0

Views: 48

Answers (1)

Akshay patil.
Akshay patil.

Reputation: 97

AS you see instance is changing for each loop. hence you will have multiple responses. Try making an array of response as shown in below: NOTE: I am assuming that $list[] is a set of SMS in this solution.

foreach($result as $row)
{
$token = $row['token'];
$version = "v1"; //DONT change unless you are using a different version
$instance = new BongaTech($token, $version);
$sms = new Sms($sender, $phone, $message, $correlator, null, $endpoint);
$response[] = call_user_func_array(array($instance, "sendBatchSMS"), $sms);
}
$response = json_encode($response, true);
print_r($response);

Upvotes: 1

Related Questions