Top Gun
Top Gun

Reputation: 688

Loop an array withing a String

I am facing a problem using arrays. I want to loop an array withing a string.

Here is my Array

Array
(
    [0] => Product: 5 M Steel Pontoons: Quantity 10
    [1] => Product: 6.7 M Steel Pontoons: Quantity 15
)

I want to iterate this array between this string.

$message = "<p> Name = $name</p>
<p>Email = $email</p>
<p>Subject= $subject</p>"

So that the string would look like this

$message = "<p> Name = $name</p>

    <p>Email = $email</p>

<p>Product: 5 M Steel Pontoons: Quantity 10</>

<p>Product: 6.7 M Steel Pontoons: Quantity 15</p>   
    <p>Subject= $subject</p>"

Any one who can help???

Upvotes: 0

Views: 45

Answers (2)

Taron Saribekyan
Taron Saribekyan

Reputation: 1390

As I understand you you can put shortecode in your $message like this:

$message = "<p> Name = $name</p>
<p>Email = $email</p>
{ADDITIONAL}
<p>Subject= $subject</p>"

then get that additional:

$additional = '';
foreach($array as $item) {
    $additional .= "<p>$item</p>";
}

than put it to message:

$message = str_replace('{ADDITIONAL}', $additional, $message);

Upvotes: 0

Happy Coding
Happy Coding

Reputation: 2525

You can try something like this :

$message = "<p> Name = $name</p>
<p>Email = $email</p>";

foreach((array) $yourArray as $key)
{
    $message .= "<p>".$key."</p>";
}

$message .= "<p>Subject= $subject</p>";

Upvotes: 2

Related Questions