简体   繁体   中英

PHP: Array with foreach loop?

I have a and Array which looks like this:

$sms = array(
  'from' => 'DummyFrom', 
  'to' => '+46709751949',
  'message' => 'Hello hello!'
);
echo sendSMS ($sms) . "\n";

what i'm trying to do is to put this array within a foreach loop so it can be executed multiple time (based on the given times from mysql database). to explain it better, I did something like this:

if (is_array($g_numbers)) {
    foreach ($g_numbers as $number) {
        $sms = array(
            'from' => 'DummyFrom',
            'to' => "" . $number . "",
            'message' => 'Hello hello!'
        );
        echo sendSMS($sms) . "\n";
    }
}

but that is wrong and its stops the PHP page to execute properly without any errors!

Could someone please advise on this issue?

My full code :

<?php
$people       = array();
$sql          = "SELECT id, g_name, numbers FROM groups WHERE g_name='$groups'";
$query        = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {

    $g_id               = $row['id'];
    $g_name             = $row['g_name'];
    $g_numbers          = $row['numbers'];
    $people[$g_numbers] = $g_numbers;

}
?>
<?
// Example to send SMS using the 46elks service
// Change $username, $password and the mobile number to send to

function sendSMS($sms)
{

    // Set your 46elks API username and API password here
    // You can find them at https://dashboard.46elks.com/
    $username = 'xxxxxxxxxxxxxxxxxxxxxxxx';
    $password = 'xxxxxxxxxxxxxxxxxxxxxxxx';

    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n" . "Content-type: application/x-www-form-urlencoded\r\n",
            'content' => http_build_query($sms),
            'timeout' => 10
        )
    ));

    $response = file_get_contents('https://api.46elks.com/a1/SMS', false, $context);

    if (!strstr($http_response_header[0], "200 OK"))
        return $http_response_header[0];

    return $response;
}
if (is_array($g_numbers)) {
    foreach ($g_numbers as $number) {
        $sms = array(
            'from' => 'DummyFrom',
            /* Can be up to 11 alphanumeric characters */
            'to' => "" . $number . "",
            /* The mobile number you want to send to */
            'message' => 'Hello hello!'
        );
        echo sendSMS($sms) . "\n";
    }
}
?>

This will work because tables values are stored in $people:

if (is_array($people)) {
  foreach ($people as $number) {
    $sms = array(
        'from' => 'DummyFrom',
        /* Can be up to 11 alphanumeric characters */
        'to' => "" . $number . "",
        /* The mobile number you want to send to */
        'message' => 'Hello hello!'
    );
    echo sendSMS($sms) . "\n";
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM