Hello dear friends,
I’ve one problem. I hope you guys help in this…!

Actually, I have two array.
One array carry ‘title’ of my sms text messages.
Second array carry ‘description of sms messages.

Problem is that…! i want to got these two array result in my foreach loop.
As you know foreach loop carry one time one array result….!
How its possible I’ll get these two array result in one loop???
Please, let me know how to declare these two array in foreach….!

thanks waiting for your reply....!

Dani AI

Generated

was right to suggest an indexed loop to keep title and description items in sync. The important additions for a robust solution are: verify both variables are arrays, handle mismatched lengths deliberately, and use a prepared statement (and a transaction for many rows) when inserting into the database.

Example using PDO with a transaction and a prepared statement:

if (!is_array($arrTitles) || !is_array($arrBodies)) {
    throw new InvalidArgumentException('Expected arrays');
}

$len = min(count($arrTitles), count($arrBodies));

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO sms (title, body) VALUES (:title, :body)');

$pdo->beginTransaction();
try {
    for ($i = 0; $i < $len; $i++) {
        $stmt->execute([':title' => $arrTitles[$i], ':body' => $arrBodies[$i]]);
    }
    $pdo->commit();
} catch (Exception $e) {
    $pdo->rollBack();
    throw $e;
}

An alternative that keeps array-index semantics but reads more clearly is a foreach on one array while using the index to fetch the matching item from the other:

foreach ($arrTitles as $i => $title) {
    $body = $arrBodies[$i] ?? '';
    $stmt->execute([':title' => $title, ':body' => $body]);
}

Notes and quick checklist (ties back to 's original arrays): ensure equal lengths or deliberately choose min(count()) to avoid undefined-index notices; do not build SQL by concatenation — use prepared statements; for large batches use transactions or multi-row INSERTs for performance; and ensure the DB connection uses the correct charset (utf8mb4) for SMS content. For reference see PHP’s documentation on count() and PDO prepared statements.

Recommended Answers

All 4 Replies

Why do you want to use a foreach loop is in this case?
If I were you I would do a for loop from 0 to size of your arrays and use that index to get the same position values in both arrays.

$sms_title = $title[0]; // this array contain sms title

$sms_detail = $detail[0]; // this array contain sms detail

foreach () {


i want to get Result of >> $sms_title

i want to get Result of >> $sms_detail

After this>>

i want to use Mysql INSERT query to insert $sms_title, $sms_detail in to database

}

***if you have another solution please, explain with example....! please..... :(

Now I don't understand what you want to do...
I thought you need something like:

$sizeOfArrays = count($sms_titles);

for ($i = 0; $i < $sizeOfArrays; $i++) {
    INSERT into DB -> $sms_titles[i] and $sms_details[i]
}
commented: Daiva Reply very helpful for me. :) +2

Daiva, thanks dear...!

you are right...! For loop is right solution...!

thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.